JavaScript Regular Expression Essence

Source: Internet
Author: User

Tag: Result lis Reference cancels space AAC func return expression

This article describes the JavaScript regular expression catalog

To create a regular expression
Regular Expression Flag
Using regular expressions in strings
Methods of regular expressions
Metacharacters
Special characters
Greedy mode
Group

  1. To create a regular expression

    var="I love English";var=newRegExp("love","g"); 使用构造函数创建var=/love/g; 使用字面量形式
  2. Regular Expression Flag

    var=/Love/i;g 全局匹配u uicode模式y 粘连模式    使用粘连模式,只有指定了正确lastIndex,才会匹配元素    var="aa-aa-aa";    var=/-/y;    reg.lastIndex=2;    console.log(reg.exec(str))
  3. Using regular expressions in strings
    • Search

      返回给定要查找的字符第一个出现的索引位置var="I love English";var=/Love/i;console.log(str.search(reg));->2
    • Match

      Returns information about a string match that does not use regular G to match only the first satisfied elementvarStr= "I Love 中文版";    varReg= /love/i;    Console.Log(Str.Match(REG));  -["Love",Index: 2,Input: "I Love 中文版"] Use parentheses to extract partvarReg= /( Love) /I;    Console.Log(Str.Match(REG));  -["Love", "Love",Index: 2,Input: "I Love 中文版"] When G is turned on using regular G, the global lookup matches the elementvarStr= "AA AA AA";    varReg= /aa/g;    Console.Log(Str.Match(REG));  -["AA", "AA", "AA"] Use parentheses to return NULL if no matching element is used
    • Split

      分割字符串var="aaCDaaCDaa";console.log(str.split(/CD/));
    • Replace

      Replace a string partvarStr= "Aa-aa-aa";Console.Log(Str.Replace(/-/, "A"));Replace the first one"Aaaaa-aa"Console.Log(Str.Replace(/-/g, "A"));Replace All"AAAAAAAA"The second argument can use the inverse reference character $$ to represent a $ characterConsole.Log(Str.Replace(/-/g, "$$"));  -AA$AA$AA $&Represents a match to a characterConsole.Log(Str.Replace(/-/g, "$&$&"));  -Aa--Aa--AA $' represents all characters before the match to the character Console.log (Str.replace (/-/g, "C $"")); AACAAAACAA-AAAA$' represents all characters after the match to the character Console.log (Str.replace (/-/g, "C $")); Aacaa-aaaacaaaa$n a character that matches the number of parentheses in the matching characterConsole.Log(Str.Replace(/(-)/g, "C$1"));  -Aac-Aac-AA The second parameter can also use a function to call this function whenever a match is madeConsole.Log(Str.Replace(/(-)/g, function(...obj){        Console.Log(obj);  -["-", "-", 2, "Aa-aa-aa"] obj parameter of the first argument, match the character of the second argument, the character of the parenthesis, the third argument, the index bit of the matching character, the fourth argument, the original characterreturn 1    }));  -Aa1aa1aa
  4. Methods of regular expressions
    • Test

      判断是否有符合正则匹配的元素console.log(/a/.test(str));->trueconsole.log(str.search(/a/!=-1);
    • Exec

      exec is used to extract matching elements without using g, and the match method without G is the samevarStr= "Aa-aa-aa";    Console.Log(/-/.exec(str));  -["-",Index: 2,Input: "Aa-aa-aa"] using G, repeat call exec to step back to findvarStr= "Aa-aa-aa";    varReg= /-/g;    Console.Log(Reg.exec(str));  -["-",Index: 2,Input: "Aa-aa-aa"]Console.Log(Reg.exec(str));  -["-",Index: 5,Input: "Aa-aa-aa"] Find all the matching elements LetResult= NULL;     while(Result= Reg.exec(str)){        Console.Log(Result);    }
  5. Metacharacters

    \d 数字0~9\s 空字符spaces,tabs,newlines\w 字母 a-ZA-"1 11 111 22".match(/\b\d\d\b/g-> ["11","22"]\D 非数字字符\S 非空格字符\W 非字母字符\B 非边界. 匹配任意字符,除了newlines
  6. Special characters

    The character to be escaped. ( ) \/ [ ][]usage    [ABC]represents one of several characters    [A-z]    [^ABC]indicates a character other than ABCquantifier    \d{5}represents 5 digits    \d{3,5}represents a >=3 <=5 character    \d{3,}represents a >=3 character    +one or more    ?0 or one    *0 or moreora|b|CExact Match    /^abc$/
  7. Greedy mode

    When using quantifiers, the default is to turn on greedy mode LetStr= "Today is a ' good day ', isn ' t it?"    Console.Log(Str.Match(/'.+'/));  -["' Good Day ', isn '",Index:  One,Input: "Today is a ' good day ', isn ' t it?"] Remove greedy modeConsole.Log(Str.Match(/'.+?'/));  -["' Good Day '",Index:  One,Input: "Today is a ' good day ', isn ' t it?"]Console.Log(Str.Match(/'[^ ']+'/)) -["' Good Day '",Index:  One,Input: "Today is a ' good day ', isn ' t it?"]
  8. Group

    Use parentheses to extract groups LetStr= "Aa-bb-aa";    Console.Log(Str.Match(/(a)+/));  -["AA", "a",Index: 0,Input: "Aa-bb-aa"] To cancel the capture groupConsole.Log(Str.Match(/(?: A)+/));  -["AA",Index: 0,Input: "Aa-bb-aa"] Sub-groups can be referenced directly in regular expressionsConsole.Log(Str.Match(/(a) \1/));  -["AA", "a",Index: 0,Input: "Aa-bb-aa"]

JavaScript Regular Expression Essence

Related Article

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.