Javascript: string method for Pattern Matching

Source: Internet
Author: User

Some methods of string are used to perform pattern matching, retrieval, and replacement operations in regular expressions. String supports four methods using regular expressions:

① Search ():

This method uses a regular expression as a parameter to return the starting character position of the first matched substring.If no matched substring exists, it returns-1. For example, the following call returns 4:

"JavaScript".search(/script/i);  //4

If the search () parameter is not a regular expression, it is first passed to the Regexp constructor and converted to a regular expression. Search () does not support global search because it ignores the flag of the regular expression parameter.

② Replace ():

This method performs the search and replacement operations. ItsFirst ParameterIs a regular expression,Second ParameterIs the string to be replaced. It will retrieve the string that calls it and match it according to the specified pattern. If the flag is set in the regular expression, this method replaces all the child strings that match the pattern with the replacement string, otherwise, it only replaces the first detected substring that matches the pattern. If the first parameter of Replace () is a string rather than a regular expression, this method will directly retrieve the string, instead (
In this way, the Regexp () constructor is used to convert it into a regular expression. For example, we can use Replace () to unify all JavaScript (Case Insensitive) in the text string into "JavaScript" using the following method ":

<script>          var text="javascript and JAVAscript and c++";          alert(text.replace(/javascript/gi,"JavaScript"));  </script> 

However, replace () is far more powerful than the previous example. The second parameter of the Replace () method can be a function that dynamically calculates the replacement string. Replace () has other important features, which are not described here.

③ Match ():

The match () method is the most common regular expression method of string.Its unique parameter is a regular expression.(Or pass its parameters to the constructor Regexp () to convert them into regular expressions ),The returned result is an array containing matching results..If the regular expression sets the flagThe array returned by this method contains all the matches in the string. For example:

<script>        alert("1 plus 2 equals 3".match(/\d+/g));  //1,2,3</script>

If no flag is set for this regular expression, Match () does not perform global search. It only retrieves the first match. However, even if match () does not perform a global search, it returns an array. In this case, the first element of the array is the matched string, and the remaining element is the child expression enclosed in parentheses in the regular expression. Therefore, if match () returns an array a, a [0] stores a complete match, A [1] Stores substrings that match the first expression enclosed in parentheses, and so on. To replace (
A [n] Stores $ N content.

For example, use the following code to parse a URL:

Finally, we should understand a feature of match. It returns the same array as other arrays.Length attribute. If match () acts on a non-global regular expression, the returned array also contains two other attributes --Index and InputThe former contains the position where the start character is matched in the string, and the latter is a copy of the target string. In the above Code, the value of the result. index attribute should be equal to 17, because the matched URL starts from the position of 17th characters in the text. The property result. input should have the same string as the variable Text. If there is no positive expression of g, the expression RIS the same as the serial number, and the value returned by s.match(r.exe is the same as that returned by r.exe C (s. We will discuss regexp.exe C (
) Method.

④ Split ():

This method breaks down the string that calls it into a substring array, and the Delimiter is its parameter.. For example:

"123,456,789".split(",");  //123,456,789

The split () method can also use a regular expression as a parameter. This capability makes this method more powerful. For example, you can specify a separator to allow multiple blank characters on either side:

"1,2, 3 , 4  ,5".split(/\s*,\s*/);  //1,2,3,4,5

The split () method has other features, which are not described here.

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.