A daily summary of javascript Learning (RegExp objects)

  • 2020-11-18 05:15:08
  • OfStack

1, regular expression test method


var text = "cat, bat, sat, fat";  
  var pattern = /.at/;
  
  if (pattern.test(text)){
   alert("The pattern was matched.");
  }

2. Regular toString() method


var pattern = new RegExp("\\[bc\\]at", "gi");
  alert(pattern.toString()); // /\[bc\]at/gi
  alert(pattern.toLocaleString()); // /\[bc\]at/gi

3, RegExp Constructor (constructor) Properties (attribute)


 var text = "this has been a short summer";
  var pattern = /(.)hort/g;
  
  /*
   * Note: Opera doesn't support input, lastMatch, lastParen, or multiline.
   * Internet Explorer doesn't support multiline.
   */  
  if (pattern.test(text)){
   alert(RegExp.input);    //this has been a short summer
   alert(RegExp.leftContext);   //this has been a   
   alert(RegExp.rightContext);  // summer
   alert(RegExp.lastMatch);   //short
   alert(RegExp.lastParen);   //s
   alert(RegExp.multiline);   //false
  }
  input  Saves the searched string 
  index  Saves the position of the matching first character 
  lastIndex      Save the matching string under 1 The position of two characters 
  lastMatch      Saves the string that matches 
  lastParen      Save the last 1 The number of matched strings (last 1 The contents in brackets) 
  leftContext     Save the contents to the left of the matching string 
  rightContext    Saves the content to the right of the matching string 
  $1 ~ $9    Save the first one 9 Submatch (contents in parentheses) 

 var text = "this has been a short summer";
  var pattern = /(.)hort/g;
  
  /*
   * Note: Opera doesn't support short property names.
   * Internet Explorer doesn't support multiline.
   */  
  if (pattern.test(text)){
   alert(RegExp.$_);    //this has been a short summer
   alert(RegExp["$`"]);   //this has been a   
   alert(RegExp["$'"]);   // summer
   alert(RegExp["$&"]);   //short
   alert(RegExp["$+"]);   //s
   alert(RegExp["$*"]);   //false
  }
  *  There are long attribute names and short attribute names 
  * input   $_   Recently, 1 Secondary matching string 
  * lastMatch  $&   Recently, 1 The next match 
  * lastParen  $+   Recently, 1 Secondary matching capture group 
  * leftContext  $`  input In a string lastMatch Previous text 
  * multiline  $*   A Boolean value indicating whether all expressions use multi-line mode. 
  * rightContext $'  input In a string lastMatch Later text 

4. Regular $1... $9


 var text = "this has been a short summer";
  var pattern = /(..)or(.)/g;
    
  if (pattern.test(text)){
   alert(RegExp.$1);  //sh
   alert(RegExp.$2);  //t
  }
   Every time to produce 1 When the parentheses are successfully matched, $1...$9  The value of the property is modified.  
   Can be found in 1 Any number of bracketed child matches are specified in a regular expression pattern, but only the most recent can be stored 9 A. 

5, RegExp exec ()


var text = "mom and dad and baby";
  
  var pattern = /mom( and dad( and baby)?)?/gi;
  var matches = pattern.exec(text);
  
  alert(matches.index); //0  The first 1 The positions that are matched to 
  alert(matches.input); //"mom and dad and baby"  Matches the original string 
  alert(matches[0]);  //"mom and dad and baby"  Match the first 1 A value 
  alert(matches[1]);  //" and dad and baby"  Match the first 2 A value 
  alert(matches[2]);  //" and baby"    Match the first 3 A value 

 var text = "cat, bat, sat, fat";  
  var pattern1 = /.at/;
  
  var matches = pattern1.exec(text);  
  alert(matches.index); //0
  alert(matches[0]);  //"cat"
  alert(pattern1.lastIndex);//0

  matches = pattern1.exec(text);  
  alert(matches.index); //0
  alert(matches[0]);  //"cat"
  alert(pattern1.lastIndex);//0

  var pattern2 = /.at/g;
  
  var matches = pattern2.exec(text);  
  alert(matches.index); //0
  alert(matches[0]);  //"cat"
  alert(pattern2.lastIndex);//0

  matches = pattern2.exec(text);  
  alert(matches.index); //5
  alert(matches[0]);  //"bat"
  alert(pattern2.lastIndex);//0

6. RegExp instance attribute


 var pattern1 = /\[bc\]at/i;
  
  alert(pattern1.global);  //false // Whether to set global lookup 
  alert(pattern1.ignoreCase); //true  Ignoring case or not 
  alert(pattern1.multiline); //false  Whether to set multi-line lookup 
  alert(pattern1.lastIndex); //0  1 Integers. Mark the beginning 1 The character position of the secondary match. 
  alert(pattern1.source);  //"\[bc\]at"  The source text of the regular expression. 

  var pattern2 = new RegExp("\\[bc\\]at", "i");
  
  alert(pattern2.global);  //false
  alert(pattern2.ignoreCase); //true
  alert(pattern2.multiline); //false
  alert(pattern2.lastIndex); //0
  alert(pattern2.source);  //"\[bc\]at"

The above is the summary of today's javascript study, which will be updated every day. I hope you will continue to pay attention to it.


Related articles: