Precautions for regular expressions and pattern in JS, regular expressions pattern
Create a RegExp object:
The regular expression can be used to create a direct amount, that is, the characters enclosed by the slash. However, in an environment where parameter changes are required, RegExp () constructor is a better choice:
var reg1 = /'\w+'/g;var reg2 = new RegExp('\'\\w+\'','g');
Compare the two creation methods. The first parameter in RegExp is the regular string to be created. Note that because it is not a direct representation, the slash "/" is not included; instead, the quotation mark "'" And the Escape Character "\" must be escaped twice in the string.
In addition, both the direct amount and the RegExp () constructor generate a new RegExp object and assign it to the variable.
As mentioned in the Javascript authoritative guide, ECMAscript 3 specifies that the same RegExp object will be returned every time a regular expression is directly transferred, therefore, regular expressions created with a direct volume will share an instance. It is not specified that different instances are returned each time until ECMAScript 5.
In various browsers, IE has always followed the rules in ECMAScript 5, while older versions of other browsers follow the rules in ECMAScript 3. Therefore, in practical applications, it is safer to create a constructor, or remember to return to 0 when using the lastIndex attribute.
Use of parentheses:
1. Group
That is, separate items are combined into subexpressions for unified processing, which is generally used? , +, *, {N, m}, and so on. See the example below:
Var reg =/Java (script )? /;
Statement.
2. Backward reference
That is, the text matched in the brackets in the quote expression with "\ n" (n stands for the referenced serial number. See the example below:
Var reg =/(\ d +) [a-z] {3} \ 1 /; // 20man20 // 20man23 error // reg =/\ 1 [a-z] {3} (\ d +)/; Error
Note that "\ n" references the matched text "20" instead of the matched regular expression "\ d + ". In addition, JavaScript can only reference the matching text. For example, if \ 1 is written before the brackets in the example, no text is matched and an error is prompted in the browser. Similarly, JS does not support "(? <Name> exp) "(exp is a regular character) Reference naming rule. Only numbers can be referenced.
Since grouping and reference are mentioned, if you only want to group and do not want to reference, it is available "(? : Exp) "format, neither matching text nor referencing numbers. See the example below:
var reg = /(\w{3})(?:\d+)([a-z]{2})\2/;//man7788abab
Obviously, \ 2 matches "AB" instead of "7788 ". This facilitates grouping and improves query efficiency.
3. subpattern matching
Sometimes we want to directly reference this article that matches the operating brackets, then we can use the sub-pattern matching function (the Sub-pattern matching in the authoritative guide is a bit awkward, actually, replace the matched text in the form of a variable ). The basic form is to use '$ n' to replace the text matching number n. It is often used in the replace () method of the String object. For example, exchange words on both sides of the equal sign:
var reg = /(\w+)=(\w+)/;var str = ‘love=hate';str.replace(reg,'$2=$1');//"hate=love"
Order, greed, laziness:
What are the common repeated matching characters? , +, *, {N, m} Use greedy match to match as many result characters as possible. It corresponds to a lazy match, that is, as few matching results as possible. You only need to add a question mark after repeated matching characters "? "You can, Such ?? , +? ,*? , {N, m }?. See the example below:
var str = 'goooogle‘;var reg1 = /o+/; //"goooo"var reg2 = /o+?/; //"go"
Now we have slightly changed the example:
var str = 'goooogle‘;var reg1 = /o+gle/; //"oooogle"var reg2 = /o+?gle/; //"oooogle"
The example result after the change is the same. Why/o +? What if gle/does not match "ogle? The regular expression always matches from left to right, and does not obtain substrings from the right for matching.
Although the above results are the same, the matching principle is not the same. In reg1, o + matches all "o" first, and then matches "gle" to complete the overall matching. In reg2, o ++? It matches an "o" first, and then the gle fails to match the string between the 2nd-bit and the 4th-bit (that is, the "ooo" of the original string. Go back to o +? Match the second "o". After successful match, match "gle" from 3rd to 4th, and so on ...... Finally, the entire string is matched.
In general, remember that, in terms of priority, the order from left to right matches> greedy/lazy matches.
Assertion with Zero Width:
For an overall explanation of assertion with Zero Width, refer to the regular expression 30-minute getting started tutorial. It is worth noting that JS only supports assertion with zero width. That is, the zero-width positive prediction first asserted "(? = Exp) "and zero-width-negative prediction first asserted" (?! Exp )".
The so-called "Zero width" means that it does not occupy space in the matching result characters. For example, "\ w" and "\ s" occupy one or more spaces, which are determined by the length of matching characters. The first and last positions such as "^" and "$" do not occupy space, and Zero Width belongs to this category.
Positive/negative prediction refers to the situation where the requirements in assertions are met. "Positive" indicates that exp is satisfied, and "negative" indicates that exp is not satisfied.
The so-called "first" means that the matched string is in front, and the zero-width assertion follows. Whether the last part of the string meets the assertions.
The so-called "assertion" is the condition of judgment.
Let's look at two examples of zero-width assertions:
var str = 'java coffeescript';var reg1 = /\b\w+(?=script\b)/; //coffee var reg2 = /\b\w+(?!script\b)/; //java
Reg1: 0-width positive prediction first asserted ,"(? = Script \ B) "indicates that a word must end with" script ". It represents a condition that does not occupy any space.
Similarly, the reg2 0-width-negative prediction first asserted ,"(?! Script \ B) "indicates words that do not end with" script.
In addition, because there is no backgu assertion with Zero Width, it is impossible to determine what conditions the first part of a string meets. But it can be implemented using multiple regular expressions in JS: first match the string to be searched, and then intercept the character substrings starting with index, then match the substring to see if it meets the asserted condition. For more information, see.
Similarities and differences between match () and exec:
Match and exec are common methods for regular expressions to match strings. The two functions are similar, with some minor differences:
1. Usage
Match is a String packaging object method. Usage: String. match (RegExp );
Exec is a regular expression object method. Its usage is RegExp.exe c (String );
2. Returned results
When the global flag "g" is not set for RegExp:
The returned results are the same. That is, if no matching value exists, null is returned. If a matching value exists, an array (array) is returned ). Array [0] is a matched string, array [1], array [2]… Corresponding to the substring $1, $2… matched by parentheses in the regular expression ....... At the same time, the array has two attributes. array. index indicates the initial position of the matching string, and array. input indicates the string being retrieved.
When the global flag is set for RegExp:
Match returns an array when there is a value. Each item in the array indicates all matched strings in sequence, so there is no child string matching parentheses. At this time, the array does not have the index and input attributes.
Exec is similar to "g" without a global label. Array is returned, array [0] is the currently matched string, array [1], array [2]... It is the string matching the parentheses of the current match. Pay attention to the lastIndex attribute of the RegExp object, indicating the last position at the end of the matched string in the original string. If no matching result is found, the lastIndex attribute is set to 0. Therefore, use the lastIndex loop to find all matching strings. Let's take a look at the example:
var str = 'I love1 my job22';var reg = /\b[a-z]+(\d+)\b/g;array = str.match(reg);//array = ["love1", "job22"] //array.index = undefind//array.input = undefined------------------------------------array = reg.exec(str);//array = ["love1", "1"]//array.index = 2//array.input = "I love1 my job22"//reg.lastIndex = 7//run againreg.exec(str);//array = ["job22", "22"]//array.index = 11//array.input = "I love1 my job22"//reg.lastIndex = 16//run againreg.exec(str);//reg.lastIndex = 0
Finally, considering the differences between ECMAScript 3 and ECMAScript 5, remember to manually set the lastIndex attribute of the RegExp object to 0 after each matching to meet the requirements of the old non-IE browser.
Js regular expression, pattern, precautions
In one sentence, use,/\ w + @ + \ w + (\. + \ w +) {1 ,}/. test (str), do not use "\ w + @ + \ w + (\. + \ w +) {1 ,}". test (str) to verify;
Directly, use add @ dfddf to pass the verification;