A day of JavaScript learning Summary (RegExp object) _javascript Tips

Source: Internet
Author: User

1. Test method of regular expression

var text = "Cat, bat, Sat, fat";  
  var pattern =/.at/;
  
  if (pattern.test (text)) {
   alert ("The pattern is 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 (properties)

 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 is 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 Save searched string index Save match first character position lastindex save matching string position of next character Lastmatch save match to String L Astparen Save the last matched string (the last bracketed content) Leftcontext Save the content on the left side of the matching string rightcontext save the content on the right side of the matching string $1~$9 save the first 9 child matches (including ) 
, the contents of the
 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 is has been a short summer
   alert (regexp["$ '"]);   This has been a   
   alert (regexp["$ '"]);   Summer
   alert (regexp["$&"]);   Short
   alert (regexp["$+"]);   S
   alert (regexp["$*"]);   False
  }
  * is divided into long attribute names and short attribute names
  * Input   $_  The most recent string to match
  * Lastmatch  $&  Last match
  * Lastparen  $+  last matched capture Group
  * leftcontext  $ '  input string lastmatch before text
  * Multiline  $*  Boolean value that indicates whether all expressions use multiline mode.
  *  The text after lastmatch in the rightcontext $ ' input string

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
  } The
  value of the $1...$9 property is modified whenever a successful match with parentheses is generated. 
  You can specify any number of parenthesized child matches in a regular expression pattern, but only the latest nine.

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 match to the position
  alert (matches.input);//"Mom and Dad and baby" match the original string
  alert (matches[0));  "Mom and Dad and baby" match the first value
  alert (matches[1]);  The second value of "and Dad and Baby"
  alert (matches[2]);   third value for "and baby" matches
 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 Properties

 var pattern1 =/\[bc\]at/i;
  
  alert (pattern1.global);  False//whether to set global lookup
  alert (pattern1.ignorecase),//true whether to ignore case
  alert (pattern1.multiline),//false whether to set up multiple-line lookups
  alert (pattern1.lastindex);//0  An integer indicating the position of the character to begin the next match.
  alert (pattern1.source);  The source text of the "\[bc\]at" 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 today's JavaScript learning Summary, and will continue to update every day, I hope you continue to pay attention.

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.