当前位置:网站首页>regular expression

regular expression

2022-07-08 01:30:00 InfoQ


 RegExp -  Regular expressions

    It can also be called  " Regular expressions "
         It's a complex data type
         effect :
          1.  It is specially used to verify whether the string conforms to the rules
          2.  Get part of the content that conforms to the rules from the string
         Usage method :
           Use some specific symbols ,  To form an expression
         Use this expression to validate the string ,  Or get some content from the string
Create a regular expression
        1.  Literal form creation
          
// 1.  Literal creation
 const reg = /abcd/;
 console.log(reg);
 console.log(typeof reg);
        2.  Built in constructor creation
// 2.  Built in constructor creation
 const reg2 = new RegExp('abcd');
 console.log(reg2);
 console.log(typeof reg2);
         significance :  The string contains a paragraph  abcd  Just letters
           testing : aaabbbcccddd  unqualified
           testing : aaabcddddd    qualified

Two methods of regular expression

       
 
1.  matching :
 
  Verify whether the string conforms to the regular rules
           Usage method :  Regular .test( The string you want to detect )
           Return value :  A Boolean value , true  perhaps  false
 
 
2.  Capture : 
  Get the fragment that conforms to the regular rules from the string
           Usage method :  Regular .exec( The string you want to capture )
           Return value :
            1.  There is no regular slice in the string
            2.  There are regular fragments in the string
             
 2-1.  Basic capture
                 The return value is an array
                [0]  It's a captured fragment
                 No matter how many clips there are ,  Just capture the first clip
                 No matter how many times you catch ,  It's the first clip
              
2-2.  When a regular expression has  ()  When
                 The return value is an array
                 from   Indexes [1]  Start with a separate capture of each parenthesis
             
 2-3.  When regular has a global identifier  g  When
                 The second capture will continue to look backward from the end of the first capture
                 Until you can't find the location ,  return  null
                 Capture again later ,  Still from  [0]  Location start search
 const reg = /abcd/;
// 1. test()
 console.log(reg.test('aaabbbcccddd'));
 console.log(reg.test('aaabcddddd'));
// 2. exec()
 const res = reg.exec('aaabbbcccddd')
 console.log(res);
 const res2 = reg.exec('aaabcdddddabcdabcd');
 console.log(res2);

  Metacharacters of regular expressions  -  Basic metacharacter

         Metacharacters :  The basic symbols that make up a regular
           Replace the text content with symbols
           Put all the text content into some symbols to replace
 
 1. \s ( A lowercase letter )   Represents a space
 1. \s
 //  Indicates that there needs to be one in the string   Space   character
 const reg = /\s/ ;//  Equivalent to  / / 
 console.log(reg.test(' abcdefg'));
 console.log(reg.test('abc d efg'));
      2. \S( Capitalization )    Represents a non space
 
// \S
 //  Indicates that there needs to be one in the string   Non blank space   character
 const reg = /\S/
 console.log(reg.test(' a'))
 console.log(reg.test(' a a a '))

      3. \t    Represents a tab (tab)
// 3. \t
//  Indicates that there needs to be a... In your string   tabs   character
 const reg = /\t/
 console.log(reg.test(' ')) //  Two spaces
 console.log(reg.test(' ')) //  A tab
       A tab is a tab ,  Not multiple spaces .
      4. \d    Represents a number
// \d
 //  Indicates that there needs to be a... In your string   Numbers   character
 const reg = /\d/
 console.log(reg.test('abcdef'))
 console.log(reg.test('ab1c1d111ef'))
      5. \D    Represents a non number
// \D
 //  Indicates that there needs to be a... In your string   The digital   character
 const reg = /\D/
 console.log(reg.test('1234567890'))
 console.log(reg.test('12a34a5a67a890'))
      6. \w    It means a   Alphanumeric underline
 
 // \w
 //  Indicates that there needs to be a... In your string   Numbers or letters or underscores   character
 const reg = /\w/
 console.log(reg.test('[email protected]#$%$#@#$%$#$%$#$%$#'))
 console.log(reg.test('[email protected]#123123$%$#@#$%$#$%asdasd$#$%$#'))
         Express   Numbers   Letter   Underline   Choose one of three   There has to be one
      7. \W    It means a   Non alphanumeric underline
// \W
 //  Indicates that there needs to be a... In your string   Alphanumeric underline   Characters other than
 const reg = /\W/
 console.log(reg.test('asdasd123123123____'))
 console.log(reg.test('asdasd123123123____%'))
 console.log(reg.test('asdasd12312312##3____'))
         Express   Numbers   Letter   Underline   Any other one will do
      8.  spot (.)  Represents any character that is not newline
//  spot (.)
 //  Indicates that there needs to be a... In your string   Non newline   Content
 const reg = /./
 console.log(reg.test('\n'))
 console.log(reg.test('a\nb'))
 console.log(reg.test(' \n'))
      9.  Oblique line (\)  The escape sign
         Transform meaningless content into meaningful content
         Transform meaningful content into meaningless content
 //  Oblique line (\)
 //  Indicates that there needs to be a letter in your string  s
 const reg = /s/
 console.log(reg.test('abcd'))
 console.log(reg.test('abcds'))

 //  Indicates that there needs to be a... In your string   Space character
 const reg2 = /\s/
 console.log(reg2.test('abcdssss'))
 console.log(reg2.test('abcdssss '))

 //  Indicates that there needs to be a... In your string   Non newline   Any character of
 const reg3 = /./
 console.log(reg3.test('asday'))

 //  Indicates that there needs to be a... In your string   spot   Text
 const reg4 = /\./
 console.log(reg4.test('asdasd'))
 console.log(reg4.test('asdasd.'))

 //  Indicates that there needs to be a... In your string  \  Text
 const reg5 = /\\/
 console.log(reg5.test('\\'))

原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/189/202207072326456348.html

随机推荐