A summary of javascript learning every day (RegExp object) _ javascript skills

Source: Internet
Author: User
This article mainly introduces the RegExp object knowledge points in javascript, including the basic usage methods and various methods of RegExp objects. For more information, see 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 Save the searched string index Save the position of the matched first character lastIndex Save the position of the matched string next character lastMatch Save the matched string lastParen save the last matched string (content in the last bracket) leftContext: Save the content on the left of the matching string. rightContext: Save the content on the right of the matching string. $1 ~ $9 Save the first nine Child matches (Content 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 } * Divided into long attribute names and short attribute names * input $ _ the string to be matched the most recently * lastMatch $ & the most recent match * lastParen $ + the most recent matched capture group * leftContext $ 'text before lastMatch in the input string * multiline $ * Boolean value, indicates whether all expressions use the multiline mode. * RightContext $ 'text after lastMatch in the input string

4. Regular Expression $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 a successful matching with parentheses is generated, $1... the $9 attribute value is modified. You can specify any number of child matches with parentheses in a regular expression, but you can only store the latest nine.

5. RegExp exec ()

Var text = "mom and dad and baby"; var pattern =/mom (and dad (and baby )?)? /Gi; var matches = pattern.exe c (text); alert (matches. index); // 0 the first matched location alert (matches. input); // "mom and dad and baby" matched original string alert (matches [0]); // The First Matching value alert (matches [1]) for "mom and dad and baby"; // The second value that matches "and dad and baby" alert (matches [2]); // The third value that matches "and baby"

 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 attributes

Var pattern1 =/\ [bc \] at/I; alert (pattern1.global); // false // whether to set global search alert (pattern1.ignoreCase ); // true: whether to ignore case-insensitive alert (pattern1.multiline); // false: whether to set alert (pattern1.lastIndex) for multi-row search; // an integer of 0, indicating the next matching character position. 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 \]"

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

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.