JavaScript regular expressions related should introduce _javascript skills

Source: Internet
Author: User
Tags array length first string
String Object
1, Str.match (REGEXP)
Search for a matching regexp string in str and save it back in an array.
If RegExp is not a global setting (/g), match only once
Copy Code code as follows:

("abc112dwfwabcwef2abc3wfwabcasqf453abcqwf24j234h"). Match (/abc\d*/g);
Results
["abc112", "abc", "ABC3", "ABC", "ABC"]

In this method, if the regular expression is not a global match pattern (g-tag), the first element will be a matching string, the remainder is the string captured in the regular, and the array has 2 properties:
Input string for instrumentation
The index-matching string is used to detect the start position in the string.
(Refer to the Regexp.exec () method for these 2 properties)
2, Str.search (REGEXP)
Returns the position of the first string that matches the regexp, and if no match returns-1, the global token does not make sense because it matches only once.
The method also ignores the RegExp lastindex property and always retrieves it from the beginning of the string.
3, Str.replace (regexp,replacetext)
Replace the string matching RegExp with ReplaceText, if the REGEXP does not have a global setting, matches only once, and all matching strings are replaced with global mode.
If a capture packet match is used in RegExp, the $ in replacetext has special meaning
$ 、...、 $99//text matching the 1th to 99th subexpression in the REGEXP.
$&//Match substring with RegExp.
$ '//text located on the left side of the matching substring.
$ '//text located on the right side of the matching substring.
$$//Match $ symbol itself.
Note that you need to consider the number of captured groupings in RegExp, and if you only set 2 captures, then $ $ will no longer have a special meaning
("123ab12c11d_4532"). Replace (/A (b\d*) C (\d*) d/, "$1@$2-")
will receive:
"123b12@11-_4532"
4, Str.replace (regexp,function)
The second parameter of the str.replace can be a function, and the return value of the function will be substituted for the matching character.
Note that the global G tag is still required if you want to match regexp globally.
The parameters of the function are, in turn:
Matches the string,
The configured catch substring (multiple),
Matches the start position of the string,
The original string used for the match
Note that you can define the number of function parameters according to the number of settings in the capture group in RegExp, and if the argument is too general, the "start position of the match string" and "the original string used for the match" cannot appear in the parameter, and of course, the arguments object is used to solve the problem Arguments (ARGUMENTS.LENGTH-2) is the starting position of the matching string, and arguments (ARGUMENTS.LENGTH-1) is the original string used for the match.
Copy Code code as follows:

var newstr = ("123ab12c11d_4532"). Replace (/A (b\d*) C (\d*) d/g,function (S,S1,S2,POS,OLDSTR) {
Return "@" +s1+ "@" +s2+ "@";
}) ;
Will get
"123@b12@11@_4532"

5, Str.split (Regexp[,limit])
Splits the string str into the array with matching strings, limit optional, to limit the length of the returned arrays
("Ada2afa4fcas6afa"). Split (/\d/,3)//"Ada,afa,fcas"
6, Regexp.exec ("str") method
Find a matching string in str, note that each time the method is run only once, match multiple needs to set REGEXP to/g and run the Exec () method multiple times, each matching return value result = Regexp.exec ("str")
Result is an array with an array length of 1, and the array element is the matched substring found,
Additionally, this array is assigned an additional 2 attributes:
Result.index represents the starting position of a matching substring at the beginning of the original string
Result.input is the original string.
Returns result = NULL when a matching substring can no longer be found, and sets the regexp.lastindex=0
Regexp.lastindex is the property of a regular expression that indicates which position of the string is currently to be matched, with an initial value of 0.
If REGEXP is set to global, after matching a string once, use the same RegExp to match a new string, manually set the Regexp.lastindex=0
If RegExp is not a global match pattern, and a loop is written in the program, and the root return value result to determine whether to terminate the match and attempt to match the string, then a dead loop is bound to be caused as long as a substring matches the matching criteria, because a Non-global match only matches the string once. Results each run match is matched to the first substring, the return result is not NULL, this is a relatively easy to make mistakes.
Copy Code code as follows:

var str = "1Visit W3school, W3school is a place to study web technology.";
var Patt = new RegExp ("W3school", "G");
var result;
document.write (patt.lastindex+ "<br/>");
document.write ("=====================================<br/>");
while (result = Patt.exec (str))!= null) {
document.write (patt.lastindex+ "<br/>");
document.write (result.constructor.name+ "<br/>");
document.write (result.length+ "<br/>");
document.write (result[0]+ "<br/>");
document.write (result.index+ "<br/>");
document.write (result.input+ "<br/>");
document.write ("=====================================<br/>");
}
document.write (patt.lastindex+ "<br/>");
Run Result:
=====================================
Array
W3school
Visit W3school, W3school is a where to study web technology.
=====================================
Array
W3school
Visit W3school, W3school is a where to study web technology.
=====================================

7, Regexp.test ("str") method
This method is similar to Regexp.exec, but returns TRUE or False only
The meaning of the regexp.lastindex is the same (this is the property of the REGEXP, regardless of whether the test method or the Exec method is used)
If the same REGEXP has used the test method and the Exec method successively, you may need to set the regexp.lastindex=0 manually by sharing the Lastindex property of the same RegExp object.
Copy Code code as follows:

var str = "1Visit W3school, W3school is a place to study web technology.";
var Patt = new RegExp ("W3school", "G");
var result;
result = Patt.test (str);
alert (result); True
result = Patt.test (str);
alert (result); True
result = Patt.test (str);
alert (result); False

ie9+, newer version of Chrome, Firefox, Str.match (reg) After the execution, regardless of whether the global match, whether there is a matching result, lastindex is reset, Reg.lastindex = 0, reg.test (str) after the execution , if the positive is not a global match, the lastindex is reset, Re.lastindex = 0 is IE8 and below, a regular match executes unless there is no matching result, otherwise Re.lastindex is the last character at the end of the match string where +1, that is, lastindex have been reset
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.