Understanding JavaScript Regular Expression _javascript tips

Source: Internet
Author: User
Tags access properties character classes parse error

Learn about RegExp types:

ECMAScript supports regular expressions by RegExp types. var expression=/pattern/flags;

Pattern section of regular expressions:

can be any simple or complex regular expression that can contain character classes, qualifiers, groupings, forward lookup, and reverse references. The meaning of various special characters (such as \,^,$,\w,\b, etc.) in regular expressions can refer to MDN regular expressions-the collation of special characters. Here we briefly introduce forward lookup and reverse reference.

forward Lookup: Regular Expressions use some characters to move forward without moving the positions of these characters, as forward search is also called positive lookup (x (? =y)) and negative forward search is also called positive negation lookup (x (?!). y)).
Reverse Reference: identifies a repeating character or string that can be supplied in a string, and can use a capture group to reverse reference matching. The numbered reverse reference \number number is the ordinal position of the capturing group in the regular expression.
1, the expression \1~\9 is interpreted as a reverse reference rather than a octal code. /\b (\w+) \s\1/.exec (' S_ s_ ');//["S_ s_", "S_"]
2, if the first digit of a multiple-bit expression is 8 or 9 (such as \80 or \91), the expression is interpreted as text. /\b (\w+) \s\80/.exec (' S_ ');//["S_", "S_"]
3. For an expression numbered \10 or greater, the expression is treated as a reverse reference if there is a reverse reference corresponding to that number. Otherwise, these expressions are interpreted as octal.

/(1) (2) (3) (4) (5) (6) (7) (8) (9) (xx\10/.exec) (' 12345678910xx10 ');//["12345678910xx10", "1", "2", "3", "4", "5", "6", "7 "," 8 "," 9 "," ten "]
/(1) (2) (3) (4) (5) (6) (7) (8) (9) (xx\11/.exec) (' 12345678910xx10 ');//null

4. If the capturing group is nested capturing groups, the order that the capturing group determines is internal from outside to inside, and from left to right. A code to experience.
/\b (\w+x (x)) \s (\1)/.exec (' s_xx s_xxstop ');//["S_xx s_xx", "S_xx", "X", "S_xx"]
5. If the regular expression contains a reverse reference to an undefined group member, a parse error occurs, and the ArgumentException is thrown by the different regular expression engines of the language. Null is returned for JavaScript. /\b (\w+) \s\2/.exec (' S_ 8 ');//null
Reverse Reference Instance code: Capturing the content of a capture group can be referenced not only by a program outside the regular expression (RegExp. $n) but also within a regular expression (\number, which is referred to as a reverse reference).

Express three consecutive lowercase letters, {2} applied to \1
/([A-z]) \1{2}/.exec (' AAA ');//["AAA", "a"]
copy code
//An interesting regular question
/(\w) (? =\ 1\1\1) (\1))/.exec (' AA bbbb ');//["BB", "B", "B", "B"]/

* There are three capturing groups in this (\w), and $ (? =\1\1\1) (\1): Need to be
aware of (?) =\1\1\1) is not a capturing group but a regular expression of the judgment condition, X (? =y) that matches x only when followed by Y, the criterion is not part of the matching result. So now the $ content is (\1) that is ' B '. $ is the content of \1. Returns the first ' B ' of the match ' BB ' as the first ' B ' in the ' AA bbbb ', the second ' B ' as the second ' B ' in the '
AA bbbb '. *

/(\w) (x (?) =\1\1\1) (\1))/.exec (' AA bxbbbcb ');//["BxB", "B", "XB", "B"]
//Here are the contents of the $ (x (?). =\1\1\1) (\1)) is the contents of X ( \1);

In fact, the above two modes can be simplified into/(\w) (=\1\1\1) (\1)/expression matching \w only after the \w followed by three \1, and then get the match for the \w and then followed by the \1 string. Empathy/(\w) x (? =\1\1\1) (\1)/
Copy Code
/(\w) ((? =\1\1\1) (\2))/.exec (' AA bbbbv ');//["B", "B", "" "," "]
* * Capture Group $ ( (? =\1\1\1) (\2)) , so \2 represents "" because the match is not done at this point in the capture group $ $. $ is the \2 content or "". So this match is interpreted as returning \w and followed by a string of 3 \w, returning \w+ ' only returns ' B '. */

The flags part of the regular expression:

You can have one or more flags to indicate the behavior of the regular expression.

1, G: Represents the global mode, the pattern will be applied to all strings, not immediately when the first occurrence is found. ' Cat mat bat '. Replace (/. =at)/g, ' A ');//"Aat Aat Aat"
2. I: case-insensitive mode, ignoring the case of the pattern and string when determining the match. ' CAt Mat bAt '. Replace (/a/gi, ' B ');/"CBt mBt bBt"
3, M: Multiline mode, when it reaches the end of a line of text, will also continue to find the next line of the existence of the matching pattern.

var str= ' Cat\nmat\nbat ';
Str.replace (/at/gm, ' AB ');
* * "CAB
mAB
BAB" * *

Part of the metacharacters in regular expressions:

These metacharacters must be escaped when they are used in the pattern, and they need to be escaped if they are to be included in the string to be matched.

( [ { \ ^ $ | ) ? * + . ] }
Match "[Bc]at"
/\[bc\]at/.exec ("Xx[bc]at");//["[Bc]at"]

//Match ". At"
/\.at/.exec ("xx.at");//[". at"] 

To create a regular expression:

literal form: like Var expression=/pattern/flags;
RegExp Constructor: two parameters (string pattern to match, optional flag string), you cannot pass regular expression literals to the constructor, although it is not an error if written. Any expression that can be defined by using a literal can be defined using a constructor. As follows:

var p=/[bc]at/;
New RegExp (' [bc]at ');///[bc]at/

1, when no parameter or parameter one is an empty string, the new RegExp ();///(?:) /or New RegExp (");///(?:) /, to match "" but not to remember the match ("" is actually ":" After the empty string, do not remember the X-match rule is (?: x)), so when matching any string, return ["]. So you can guess the JavaScript regular engine internal mechanism should be the default match "" and do not remember the match unless explicitly declared after ":" The need to match the string, plus "(?:)" Explicit declaration does not remember the match.
2. Because the constructor pattern parameter is a string, in some cases (refers to those that have been escaped) the character is double escaped (that is, the literal form of a single escape and then another layer of escape). In some cases, it is also possible to perform a single transfer (New RegExp (' \w ');///w/). Note ' \ ' is special and needs to be escaped in the string.

var p=/\\n/;//escape \, the character "\" in the string often need to be escaped to "\"
p.exec ("\\nxx"),//["\ n"]

var p=new RegExp ("\\\\n");///\\n/ If you want a regular expression literal to be/\\n/, you need to have another layer of escape
p.exec (' \\nxx ') in the regular expression,//["\ n"] notice that the matching string ' \nxx ' is also escaped with the

new RegExp (' \\n ') . exec ("\ n");// 
[["
]
/*regexp (' \\n ') returns/\n/, meaning matching line break/

new RegExp (' \ n '). EXEC (" \ n ");
"]
/*new RegExp (' \ n ') returns
/
/, indicates that it is not escaped, but returns literal
/
/, meaning matches the newline character
* *

3, the following gives a number of single weight, dual escape mode of reference: The first escape in the table has been marked out, the single representative for the initial escape, two representatives on the existing escape based on the escape.

Instance Properties for RegExp:

Various information about patterns can be obtained through the properties of an instance

Global: Boolean value that indicates whether the G flag is set.
IgnoreCase: Boolean value that indicates whether I flag is set.
Multiline: Boolean value that indicates whether the M flag is set.
Lastindex: An integer that represents the character position at which to begin searching for the next occurrence, starting with 0. This is only useful if you are setting the G flag.
Source: The string flag of a regular expression that returns a string in literal form rather than in the string pattern in the constructor.

New RegExp (' \\\\w ');///\\w/returns the form of a regular expression
new RegExp (' \\\\w '). source;//"\\w" string

Example method for regexp:

exec (): The method is designed specifically for capturing groups, and the argument is a string to match, and an array containing the first occurrence information and possible capturing groups is returned, if not matched to return null. (The returned is an instance of Array but contains two additional attributes: index represents the position of the match in the string, and input represents the string that applies the regular expression)

var arr=new RegExp (' \\\\ (w) '). EXEC (' \\w ');//["\w", "W"]
arr;//["\w", "W"] arr.index;//0
arr.input;//"\ W "is the content in exec ()

The difference between exec () and Match () methods:

1. For exec (), even if the global flag G is set in the pattern, it returns only one match at a time; the match () method of the string can return all matches without capturing the group and the returned array has no index and input properties when setting G.

2. For exec (), a capturing group can be returned, but match () cannot return a capturing group when there is no global G flag, at which point the array returned by match has the index and input properties.

Returns a global match demo compare
var arr= ' Ababcdab '. Match (/ab/g);//["AB", "AB", "AB"]
arr.index;//undefined
arr.input;// Undefined
/ab/g.exec (' Ababcdab '); ["AB"]

//Capture Group demo comparison, Match () method and there is no set global G flag about
' Ababcdab '. Match (/A (b)/g //["AB", "AB", "AB"]
var arr= ' Ababcdab '. Match (/A (b)/);/["AB", "B"]
arr.index;//0
arr.input;//' Ababcdab '/
A (b)/g.exec (' Ababcdab ');//["AB", "B"] 

3, so in the selection of the use of the method to first consider the way to focus on what aspects of the function, when you do not set global flag G, calling exec () multiple times on the same string always returns the information for the first occurrence, and when the global flag is set, each call to exec () will continue to find the new match in the string, along the last location in the lookup.

No global, find Var p=/a/every time from scratch
;
var str= ' Ababa ';
var a=p.exec (str);//["a"];
var b=p.exec (str);//["a"];
a==b;//false
a.index==b.index;//true

//set global, continue to find new matching Var p=/a/g along last position
;
var str= ' Ababa ';
var a=p.exec (str);/["a"]
a.index;//0
var b=p.exec (str);//["a"]
b.index;//2

Test (): receives a string parameter that returns true if the pattern matches the string parameter, or FALSE. Often used in the if () when the condition is judged.

var text= "000-000-000";
var p=/((\d{3})-) \1*\2/; if (p.test (text)) {
 Console.log (' match succeeded ');
}

The toLocaleString () and toString () methods of the RegExp instance inherit the literal form of the regular expression, regardless of how the regular expression was created. ValueOf () returns the literal amount of the regular expression itself.

var p=/\[new\]bi/;
P.tolocalestring ();//"/\[new\]bi/"
p.tostring ();//"/\[new\]bi/"
p.valueof ();///\[new\]bi/

var p=new REGEXP (' \\[new\\]bi ');
P.tolocalestring ()//"/\[new\]bi/"
p.tostring ();//"/\[new\]bi/"
p.valueof ();///\[new\]bi/

Constructor Properties for RegExp:

The constructor itself contains properties (static properties) that apply to all expressions in the scope and vary based on the most recent regular expression operation performed. There are long property names (the following code) and short property names (that is, the $ prefix form, since most of these symbols are not valid ECMAScript identifiers, so they cannot be accessed directly on the RegExp constructor in the form of "." and are accessed through the square brackets syntax) Two ways to access these properties

/(.) Hort/g.exec (' This was a short day ');/["Short", "s"]

///Last string to match regexp.input;//"This is a short day
" or regexp[" $_ "] visit;

The most recent match
regexp.lastmatch;//"short" or regexp["$&"] access;

The text before the most recent match in the most recent string to match is 
accessed by regexp.leftcontext;//"This is a" or regexp["$"];

The text 
regexp.rightcontext;//"Day" or regexp["$"] after the most recent match in the last string to be matched;

The most recent (last) matching capturing group
regexp.lastparen;//"s" or regexp["$+"] access;

Capturing group access properties: There are also 9 constructor properties for storing capturing groups, and the access syntax is REGEXP. $n, where n takes the value 1~9 to get the nth matching capturing group. These properties are automatically populated when you invoke exec (), test (), or match (), and other regular series methods.

var text= "This are a short summer";
var pattern =/(..) or (.) /g;
if (pattern.test (text)) {
 console.log (regexp.$1);//sh
 console.log (regexp.$2);//T 
}

The limitations of the pattern:

The attributes of some advanced regular expressions are missing, such as if backward lookup is not supported, named capturing groups (such as the string of a capturing group named name before \k<name>), and so on.

Recommended Topics : "javascript Regular expression usage instructions"

The above is the entire content of this article, I hope to help you learn.

Related Article

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.