JS Regular expression

Source: Internet
Author: User

Regular expressions
  Is a rule that allows a computer to read human rules.  it operates on strings and does not manipulate other data types. The  regular operation method is also within the scope of the string manipulation method. The  majority of the judgments in JS can be used in regular, but the implementation of the project does not do so.  so it is usually used to determine the registration of mobile phone number, mailbox, etc.  How to define a regular  Method 1
varre=//; //正则最常用,最简单的定义方法
  Method 2
varre=newRegExp(‘‘); //一般不用这种方法
  Common methods  of regular operation Test is going to match the string, successfully returning true, and not successfully returning false. So it is generally used to judge.    and, as long as the match to the string returns True, the back is no longer seen.  syntax: Regular. Test (string);  Cases:
var str= ' ABCDE ' ; var re=/b/; console.log (re.test (str)) //true re=/g/; console.log (re.test (str)) //false re=/b/; //uppercase console.log (re.test (str)) //false is a case-sensitive re=/b/i; //Plus I ignore case, note must be written on the last side ocnsole.log (re.test (str)) //true
  Some symbolic meanings in the regular:  | represents "or"  \w character  \w non-character  \d Digit  \d non-digit  \s space  \s non-space  \b Independent  \b Non-independent   A character    letter, a number, an  independent part    of an area with which it is used as the starting position, or the end position, or separated by a space. Either the    head, or the tail, or the empty open.  Example:
var str=‘122‘;var re=/\b1/;console.log(re.test(str)) //true 匹配在开头位置的1var str=‘221‘;var re=/1\b/;console.log(re.test(str)) //true 匹配在末尾位置的1var str=‘21 2‘;var re=/1\b/;console.log(re.test(str)) //true 匹配在空格在后面的1var str=‘2 12‘;var re=/\b1/;console.log(re.test(str)) //true 匹配在空格前面的1
Search
  Used to search for the corresponding string, returning the first found location, not found return-1.  global lookups are not supported.  Syntax: Str.search (RE)
varstr=‘abcde‘;varre=/e/i;console.log(str.search(re)); //4 返回找到的位置
Match saves the successful entry to the array
  Saves the matched position to the array.  syntax: string. Match (Regular)  If you need to find all, you need to use/g (global lookup)
//把查找到的内容全部保存在数组里varstr=‘agh123gh456dp789ghdphg‘;varre=/\d/;console.log(str.match(re)) //‘1‘re=/\d+/;console.log(str.match(re)) //‘123‘re=/\d+/g;console.log(str.match(re)) //[ ‘123‘, ‘456‘, ‘789‘ ]
Replace find replacement
  Syntax: string. Replace (regular, NEWSTR)
varstr=‘aaca‘;var re=/a+/g;varnewStr=str.replace(re,‘b‘)console.log(newStr) //bcb 因为有 + 匹配多个re=/a/g;newStr=str.replace(re,‘b‘)console.log(newStr) //bbcb
example, swearing to replace
  Put: This is a dirty word, this is really a dirty word for  : * is a *,* really is a sentence *
varstr=‘这是一句脏话,这真的是一句脏话‘;varre=/这|脏话/g;console.log(str.replace(re,‘*‘)); //*是一句*,*真的是一句*
  The second parameter to replace is not just the ability to use a string, but also your anonymous function.  anonymous functions can be replaced by up to 99 form parameters.  $1-$99 the text that matches the 1th to 99th sub-expression in a regular. $ A  represents the item that is currently being found.  can be $0-$99, which means
var str=‘这是一句脏话,这真的是一句脏话‘;var re=/这|脏话/g;console.log(str.replace(re,function($0){ //$0是匹配成功的项,是一种固定形式  var sl=‘‘;  for(var i=0;i<$0.length;i++){    sl+=‘*‘  }  return sl}));
example, review the sorting of strings.
//in sorted order ABCDEFG var str= BADECGF ' /*var arr=[]; for (var i=0;i<str.length;i++) { &NBSP;&NBSP; Code class= "JS Comments" >arr.push (Str[i]) }*/ Console.log (Str.split ( " Code class= "JS string" > " )
  brackets ()  The individual items are made into sub-expressions so that they can be processed with a separate unit, such as *, +, and.  such as/(AB|CD) +|ef/  /ab|cd+|ef/  \1 represents a recurring subkey.  the quantifier {}  indicates the number of occurrences.  *, +, M is also a quantifier, just because the use of a lot of simple expression.  {0,} * more than 0 times.  {1,} + 1 times or more.  {0,1}? 0 or 1 times.  {n,m} comes out at least n times, up to M times.  {n} occurs exactly n times.  {n,} appears at least n times.  character class, indeterminate []  any character.  any one of [ABC] ABC can appear.  For example:/o[sub]t/can match: Ost, out, OBT.  Range  [A-z] all lowercase letters  [0-9] All numbers  [a-z0-9] All small letters or numbers. Note, no or no | , and no spaces. Ending  ^ $  ^ start  $ end  Note: in brackets [], ^ denotes inverse.
Example, find out the most of that character
var str=‘ansssstrcssfdssssfsdddfrsserbdfssssdsss‘;var newStr=str.split(‘‘).sort().join(‘‘) //排序过console.log(newStr)var re=/(\w)(\1)+/g;var index=0; //记录出现的次数var val=‘‘; //记录谁出现的var aa=newStr.replace(re,function($1,$2){ //使用 replace 是为了使用里面的函数  console.log($1);  console.log($2); //abc没有出现,因为他没有重复出现  if(index<$1.length){    index=$1.length;    val=$2;  }})console.log(‘最多的字符是 ‘+val+‘出现了‘+index+‘次‘)
Determine the phone number
  Judging is not the cell phone number  1 beginning, the second, 3/5/7/8, a total of 11  /^1[3578]\d{9}/not add $  /^1[3578]\d{9}$/plus $  if not add $ end number, That's as long as the last match is going to continue to match, resulting in 12-bit, 13-bit or more phone number errors
vartxt=‘13123456789‘;varre=/^1[3578]\d{9}$/;re.test(txt) ? console.log(‘是手机‘) : console.log(‘不是手机‘)//^1 第1位是1//[3578] 第2位是3578中的某位//\d{9} 后面的是数字,并且有9位
Judge QQ number
  Judging QQ number  1 to 9 beginning, a total of 5 to 11 bits
varstr=‘12345123451‘;varre=/^[1-9]\d{4,10}$/;console.log(re.test(str))

JS Regular expression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.