A comprehensive analysis of the match, replace, exec functions _javascript techniques in JS strings and regular expressions

Source: Internet
Author: User
Tags modifier

A regular expression (regular expression) describes a pattern of string matching that can be used to check whether a string contains a seed string, replaces a matching substring, or extracts a substring from a string that matches a condition.

Regular expressions because they are not used frequently, so easy to forget, the following small series of commonly used functions and functions, concise listing here, for future viewing:

The functions of a RegExp object are commonly used in 2

1. Test function

Usage: regexpobject.test (String)

Return: Returns True if string strings contain text that matches regexpobject, or false.

Description: There is nothing special about this method, no special treatment for modifier g

Example:

var url = ' http://www.baidu.com?a=1&b=2&c=3 ';
var reg =/a=1/;

2. exec function

Usage: regexpobject.exec (String)

Return: Returns an array that holds the result of the match. If no match is found, the return value is null.

Describe:

The exec () method is powerful, it is a common method, and is more complex to use than the test () method and the method that supports the String object of the regular expression.

If EXEC () finds a matching text, an array of results is returned. Otherwise, NULL is returned. The No. 0 element of this array is the text that matches the regular expression. The 1th element is the text (if any) that matches the 1th subexpression of Regexpobject, and the 2nd element is the text (if any) that matches the 2nd subexpression of Regexpobject, Push. In addition to the array elements and length properties, the Exec () method returns two properties. The Index property declares the position of the first character of the matching text. The input property holds a string that is retrieved. We can see that when calling the Exec () method of a Non-global RegExp object, the returned array is the same as the array returned by the calling Method String.match ().

However, when Regexpobject is a global regular expression, the exec () behavior is slightly more complex. It starts retrieving string strings at the character specified by the Regexpobject lastindex property. When exec () finds the text that matches the expression, after the match, it sets the Regexpobject lastindex property to match the next position of the last character in the text. This means that you can iterate through all the matching text in a string by repeatedly calling the Exec () method. When exec () can no longer find the matching text, it returns null and resets the Lastindex property to 0.

Example:

Regular expression with modifier g

var url = ' http://www.baidu.com?a=1&b=2&c=3 ';
var reg =/([^?&=]+) = ([^?&=]) */g;
Console.log (reg.exec (URL)); ["A=1", "a", "1", index:21, Input: "http://www.baidu.com?a=1&b=2&c=3"]
console.log (reg.exec (URL));//[ "B=2", "B", "2", index:25, Input: "http://www.baidu.com?a=1&b=2&c=3"]
console.log (reg.exec (URL));//["c= 3 "," C "," 3 ", index:29, Input:" http://www.baidu.com?a=1&b=2&c=3 "]
console.log (reg.exec (URL));//null
reg.lastindex = 0;//This code is important oh, pay attention to understand
Console.log (reg.exec (URL)),//["A=1", "a", "1", index:21, Input:http://www . baidu.com?a=1&b=2&c=3]

Regular expression without modifier g

var url = ' http://www.baidu.com?a=1&b=2&c=3 ';
var reg =/([^?&=]+) = ([^?&=]) */g;
Console.log (reg.exec (URL)); ["A=1", "a", "1", index:21, Input: "http://www.baidu.com?a=1&b=2&c=3"]
console.log (reg.exec (URL));//[ "A=1", "a", "1", index:21, Input: "http://www.baidu.com?a=1&b=2&c=3"]
console.log (reg.exec (URL));//["a= 1 "," a "," 1 ", index:21, Input:" http://www.baidu.com?a=1&b=2&c=3 "]
console.log (reg.exec (URL));//[" A=1 ", "A", "1", index:21, Input: "http://www.baidu.com?a=1&b=2&c=3"]
reg.lastindex = 0; 
Console.log (reg.exec (URL)); ["A=1", "a", "1", index:21, Input:http://www.baidu.com?a=1&b=2&c=3]

Find a different place, the function of the description of the good read it, you understand ^_^

The function of a string object is supported by 4 of the regular, we only say that 2 of them

1, Match function

Usage: Stringobject.match (searchvalue | regexp), here we only say RegExp mode

Return value: An array that holds the matching results. The contents of the array depend on whether RegExp has global flag G.

Describe:

The match () method retrieves the string stringobject to find one or more text that matches the regexp. The behavior of this method depends to a great extent on whether the regexp has a sign G.

If RegExp does not have a flag G, then the match () method can only perform a match in Stringobject. If no matching text is found, match () returns NULL. Otherwise, it returns an array that holds information about the matching text it finds. The No. 0 element of the array holds the matching text, while the remaining elements hold the text that matches the subexpression of the regular expression. In addition to these regular array elements, the returned array also contains two object properties. The Index property declares the position of the starting character of the matching text in the Stringobject, and the input property declares a reference to the Stringobject.

If the regexp has a flag G, the match () method performs a global search and finds all the matching substrings in the stringobject. If no matching substring is found, NULL is returned. If one or more matching substrings are found, an array is returned. However, the contents of the array returned by the global match are very different from the former, and its array elements contain all the matching substrings in the stringobject, and there is no index attribute or input property.

Example:

Not with modifier g

var url = ' http://www.baidu.com?a=1&b=2&c=3 ';
var reg =/([^?&=]+) = ([^?&=]) * *;
var result = Url.match (reg);
Console.log (result); ["A=1", "a", "1", index:21, Input: "http://www.baidu.com?a=1&b=2&c=3"]
console.log (result.index);//21
Console.log (result.input);//http://www.baidu.com?a=1&b=2&c=3

with modifier g

var url = ' http://www.baidu.com?a=1&b=2&c=3 ';
var reg =/([^?&=]+) = ([^?&=]) */g;
var result = Url.match (reg);
Console.log (result); ["A=1", "b=2", "c=3"]
console.log (result.index);//undefined

Find a different place, the function of the description of the good read it, you understand ^_^

2, replace function

Usage: stringobject.replace (regexp/substr,replacement)

Return value: A new string that is replacement replaced with the first match of RegExp or all matches.

Description: The Replace () method of the string Stringobject performs a find-and-replace operation. It will look for substrings in the stringobject that match the regexp, and then replace them with replacement. If RegExp has global flag G, then the Replace () method replaces all matching substrings. Otherwise, it replaces only the first matching substring.

Replacement can be either a string or a function. If it is a string, then each match is replaced by a string. However, the $ character in replacement has a specific meaning. As shown in the following table, it shows that the string that is matched from the pattern will be used for substitution.

character Replace text
$, $ 、...、 $99 Text that matches the 1th to 99th subexpression in RegExp.
$& The substring that matches the regexp.
$` The text located on the left side of the matching substring.
$' The text that is located to the right of the matching substring.
$$ The direct measure symbol. (The meaning is to replace the $ symbol, write two $)

Example:

Not with modifier g

var url = ' http://www.baidu.com?a=1&b=2&c=3 ';
var reg =/([^?&=]+) = ([^?&=]) * *;
var url1 = Url.replace (reg,function (a,b,c,d,e) {
console.log (a,b,c,d,e);//a=1, A, 1, http://www.baidu.com?a=1& amp;b=2&c=3 return
' OK ';
}
Console.log (URL1); Http://www.baidu.com?ok&b=2&c=3

with modifier g

var url = ' http://www.baidu.com?a=1&b=2&c=3 ';
var reg =/([^?&=]+) = ([^?&=]) */g;
var url1 = Url.replace (reg,function (a,b,c,d,e) {
console.log (a,b,c,d,e);//execute 3 times, output respectively as: A=1, A, 1, http:// Www.baidu.com?a=1&b=2&c=3 and b=2, B, 2, http://www.baidu.com?a=1&b=2&c=3 and | C=3, C, 3, http://www.baidu.com?a=1&b=2&c=3 return
' OK ';
}
Console.log (URL1); Http://www.baidu.com?ok&ok&ok

When the second argument is a string

 var url = ' http://www.baidu.com?a=1&b=2&c=3 '; var reg =/([^?&=]+) = ([^?&= ])*/; No modifier g var url1 = Url.replace (Reg, "$&") Console.log (URL1); http://www.baidu.com?a=1&b=2&c=3 var url1 = Url.replace (Reg, "$") Console.log (URL1); http://www.baidu.com?a&b=2&c=3 var url1 = Url.replace (Reg, "$") Console.log (URL1); http://www.baidu.com?1&b=2&c=3 var url1 = Url.replace (Reg, "$ '") Console.log (URL1); http://www.baidu.com?&b=2&c=3&b=2&c=3 var reg =/([^?&=]+) = ([^?&=]) */g; with modifier g var url1 = Url.replace (Reg, "$&") Console.log (URL1); http://www.baidu.com?a=1&b=2&c=3 var url1 = Url.replace (Reg, "$") Console.log (URL1); http://www.baidu.com?a&b&c var url1 = Url.replace (Reg, "$") Console.log (URL1); http://www.baidu.com?1&2&3 var url1 = Url.replace (Reg, "$ '") Console.log (URL1); Http://www.baidu.com?&b=2&c=3&&c=3& 

The above is a small series to introduce a comprehensive analysis of JS strings and regular expressions in the match, replace, exec and other functions, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.