Regular expression string method

<! DOCTYPE html>
<html>
 <head>
  <meta charset = "UTF-8">
  <title> </ title>
 </ head>
 <body>
  <script type = "text / javascript">
   / *
    * regular expression String method
    * /
   // split ();
    //-You can split a string into an array
    //-The method can pass a string as a parameter, this method will be split according to the regular expression Split string
    //-This method will split all
    var str = "1a2b3c4d";
    var result = str.split (/ [Az] /);
    console.log (result);
    
    
   // search ()  
       //-You can search whether the string contains the specified content
       //-If the specified content is searched, the index of the first occurrence will be returned, if it is not searched, -1 will be returned;
       //-it can accept A regular expression is used as a parameter, and then the string will be retrieved according to the regular expression.
       // search () will only find the first one, even if you set a global match, it is useless
       var str = "hello abc hello aec";
       // Search whether a string contains abc or aec or afc
       var result = str.search (/ a [be] c /);
       console.log (result);
      
      
   // match ()
   //-According to the regular expression, the content that meets the conditions can be extracted from a string
   //-By default, our match will only find the first character and the required content, and stop searching after it is found
   / /    -We can set the regular expression to the global matching mode, so that it can match all the content
   //-We can set multiple matching modes for a regular expression, and the order does not matter
// -match () will match the content Encapsulated and returned to an array, even if one is found, the array is
   var str = "1a2b3c4d5e";
   var result = str.match (/ [Az] / g);
// var result = str.match (/ [az] / ig);
   console.log (result);
// console.log (Array.isArray (result)); judge whether the result is an array
   
   
   
   // replace ()
//-can replace the content specified in the string with new content
// -parameters
   // 1. The content to be replaced can accept regular expressions as parameters
    // 2. New content
    var str = "1a2b3c4d5e";
// var result = str.replace ("a", "@-@");
    var result = str.replace (/ [az] / ig, "" ); // Delete the letters
    console.log (result);
  </ script>
 </ body>
</ html>

Guess you like

Origin www.cnblogs.com/weixin2623670713/p/12747296.html