js-Regular Expressions

Source: Internet
Author: User

The metacharacters of regular expressions are characters that contain special meanings, and they have special functions that control how the pattern is matched, and the meta-character after the backslash loses its special meaning.

  • single character and number
    • .Represents a single character that matches a line break except for a newline character, and two . indicates a match of two characters
      var pattern1=/g.gle/; alert(pattern1.test(‘gogle‘))//true alert(pattern1.test(‘g\ngle‘))//换行符false alert(pattern1.test(‘ggle‘))//没有匹配false var pattern2=/g..gle/; alert(pattern2.test(‘google‘))//true alert(pattern2.test(‘gooogle‘))//false
    • [a-z0-9A-Z]
      • [a-z]Represents 26 lowercase letters any one of them matches
        var pattern1=/[a-z]oogle/;  alert(pattern1.test(‘hoogle‘))//true  alert(pattern1.test(‘google‘))//true
      • [A-Z]Represents 26 uppercase letters any one of them matches
        var pattern1=/[A-Z]oogle/;  alert(pattern1.test(‘Aoogle‘))//true  alert(pattern1.test(‘google‘))//false
      • [0-9] represents 0-9 any number matching
        var pattern1=/[0-9]oogle/; Alert (pattern1.test ( 000000oogle ') //true alert ( Pattern1.test ( ' Google ') //flase    
      • [a-za-z0-9] means matching any letter and number
         var pattern1=/[a-za-z0-9]oogle/; Alert (pattern1.test ( 0oogle ') //true Alert (Pattern1.test ( Span class= "hljs-string" > ' Google ') //true Alert (Pattern1.test (  Foogle ')) //true          
        "Use with repeating characters" means that you can match any number of characters
         var       Pattern1=/[0-9]*oogle/; Alert (pattern1.test ( ' oogle ') //true Alert (Pattern1.test ( Span class= "hljs-string" > 00000google)) //true      
    • [^a-z] denotes non- var pattern1=/[^a-z]oogle/; alert (pattern1.test //true alert (pattern1.test ( google ') //flase alert (pattern1.test ( ' Foogle ')) //true Of course it can be any non-uppercase, non-numeric, not ~
    • \w小写Indicates a matching alphanumeric number and _
      var pattern1=/[a-zA-Z0-9_]oogle/;//可以被后者取代 var pattern2=/\woogle/; alert(pattern2.test(‘_oogle‘))//true alert(pattern2.test(‘.oogle‘))//false
    • \W大写is a lowercase negation, which means matching any non-alphanumeric underscore
    • \dMatch numbers
      var pattern1=/[0-9]oogle/; var pattern2=/\doogle/; alert(pattern2.test(‘2oogle‘))//true alert(pattern2.test(‘.oogle‘))//false
    • \DUppercase Take not
  • White space characters

    • \sIndicates a blank match
      var pattern1=/goo\sgle/; alert(pattern1.test(‘goo gle‘))//true
  • Anchor character

    • ^[]Put it outside the brackets. Indicates that the content within the brackets in the restriction appears at the beginning of the string,
    • []$Indicates that the contents of the brackets in the restriction, or a character immediately adjacent to it, need to appear at the end of the string
       var pattern1= /^google$/; alert (pattern1.test ( ' Google ')) //true var pattern2=/^google/; Alert (pattern2.test ( ' googleeeeee ')) //true var pattern3=/google$/" alert (pattern3.test (//true var pattern1= /^google[0-9]$/; alert (pattern1.test ( ' google7777 ')) //false alert (pattern1.test ( ' Google7 ')) //true             
    • \bIndicates whether the boundary is reached
      var pattern1=/google\b/; alert(pattern1.test(‘google‘))//true var pattern1=/google\b/; alert(pattern1.test(‘googlee‘))//flase
  • repeating characters
    • * means match 0 or any number of * preceding characters
       var pattern1=/go*gle/; Alert (pattern1.test (  Ggle ')) True Alert (pattern1.test ( ' Gogle '))//true alert (pattern1.test (      
    • + means match 1 or more preceding characters
      var Pattern1=/go+gle/; Alert (pattern1.test (  Ggle ')) False Alert (pattern1.test ( ' Gogle '))//true alert (pattern1.test (      
    • Indicates a match of 0 or one? The preceding character
      var pattern1=/go?gle/; alert(pattern1.test(‘ggle‘))//true alert(pattern1.test(‘gogle‘))//true alert(pattern1.test(‘gooogle‘))//false
      This is the repetition limit for any character.
      var pattern1=/g.?gle/; alert(pattern1.test(‘ggle‘))//true alert(pattern1.test(‘gbgle‘))//true
    • {m,n}Match m~n including M with N
      var pattern1=/go{2,4}gle/; alert(pattern1.test(‘gooooogle‘))//false alert(pattern1.test(‘google‘))//true
      Indicates that only a match qualifies several
      var pattern1=/go{2}gle/; alert(pattern1.test(‘gooooogle‘))//false alert(pattern1.test(‘google‘))//true
      That matches at least one
      var pattern1=/go{2,}gle/; alert(pattern1.test(‘gooooogle‘))//true alert(pattern1.test(‘google‘))//true
    • ()+匹配至少一个模式
      var pattern1=/(google)+/;//对google这个字符串进行重复匹配 alert(pattern1.test(‘googlegooglegoogle‘))//true
  • or character
    or pattern matching
     var pattern1=/google|baidu|bing/; or pattern matching alert (pattern1.test ( ' This is Baidu ') //true alert ( Pattern1.test ( ' Soso ') //false     
  • Alternate character
     var Pattern1=/(. *) \s (. *)/;var str= ' Google Baidu '; alert (Str.replace (Pattern1,//baidu Google var pattern1=/8 (. * ) 8/; //get 88 any character between var str= ' this is 8google8 '; document.write (str.replace (Pattern1, "<strong>$1 </strong> ')              
  • record character
    • group
       var pattern1=/(google) {2,4}/; Repeat match alert for Google's string (pattern1.test ( ' Googlegooglegoogle ')) //true alert (pattern1.test ( ' Google ')) //flase         
    • Capture grouping
      var Pattern1=/8 (. *) 8/; //get 88 of any character between alert (Pattern1.test ( ' this is 8google8 ')) //truealert (regexp.$1); //google means that the matching string corresponding to the first group in the get pattern must be run. 

Greed and inertia

  • Greedy mode
      var pattern=/8(.*)8/;// 使用了贪婪,匹配到了google8 8google8 8google  var str=‘8google8 8google8 8google8‘; document.write(str.replace(pattern,‘<strong>$1</strong>‘))
  • Lazy mode
     var Pattern=/8 (. *?) 8/ G //lazy mode, all matching var str= ' 8google8 8google8 8google8 '; document.write (str.replace (Pattern, <strong>$1< /strong> ')         
    add one after * Represents the elimination of greedy mode, in lazy mode, only matching one. The
    below is not in lazy mode, except that the matching results are removed from the 8
     var pattern=/8 ([^8]*] 8/g; //lazy mode, all matching var str= ' 8google8 8google8 8google8 '; document.write (str.replace (Pattern, <strong>$1< /strong> ')         
  • capture vs. non-capture
    When grouping is not added:. Exec returns only the string that matches the current position
      var pattern=/[a-z]*\s\d{4}/;  var str=‘google 2015‘;  alert(pattern.exec(str));//google 2015
    After the grouping is added, the grouped content is captured and assigned to the array returned by exec
      var pattern=/([a-z]*)\s(\d{4})/;  var str=‘google 2015‘;  alert(pattern.exec(str));//google 2015,goole,2015
    Non-capturing or ungrouped grouping is captured(?:)By adding a question mark and a colon before grouping, you can cancel capturing the group.
      var pattern=/([a-z]*)\s(?:\d{4})/;  var str=‘google 2015‘;  alert(pattern.exec(str));//google 2015,goole
  • Grouping Nesting
    Nested groupings need to be taken from the Inside Out ~
      var pattern=/(a?(b?(c?)))/;  var str=‘abc‘;  alert(pattern.exec(str));//abc,abc,bc,c
    First step: Get the matching string: ABC
    Step two: Get the first group, which is the outermost: ABC
    Step three: Get the second group, BC
    Fourth step: Get the inner layer Group C
  • Use prospective capture
      var pattern=/goo(?=gle)/;  var str=‘google‘;  alert(pattern.exec(str));//goo
    A pair () is added after the regular matched string, and the matching condition is defined within the parentheses, which can be matched only if the condition is met. Note that the last match value for this example is goo.
  • Match with special characters
      var pattern=/\[/;  var str=‘[‘;  alert(pattern.test(str));//true
    For a particular case to be matched, it needs to be preceded by a slash to identify it.
  • Line break mode

     var pattern=  /\d+/g;  var str= ' 1, abc\n2, EDF '; Alert (str.replace (Pattern,var pattern=/^\d+/g; var str= ' 1, abc\n2, EDF '; alert (Str.replace (Pattern,//matches the first var pattern=/^\D+/GM; Span class= "Hljs-keyword" >var str= ' 1, abc\n2, EDF '; alert (str.replace (Pattern,//exactly matches and replaces             

    When a newline character is present in a string, if we do not use anchor characters, it will match the global, including after line break.
    However, because of the use of anchor characters, there is no way to match the result after a newline, even if the global is used, and you need to turn on line breaks at this point.

js-Regular Expressions

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.