Clarify JavaScript Regular expressions

Source: Internet
Author: User

Clarify JavaScript regular Expressions--next

immediately:"clear javascript Regular Expression--the previous article."

The application of regular in the String class

The class string supports four methods that utilize regular expressions. The search, replace, match, and split methods are respectively. Here's a story.

--string.search (regexp)--

Search relative to the other three methods, is the simplest way to apply the regular, the function is to find the corresponding matching pattern according to the parameter regexp.

Specific as follows:

The parameter in search is a regular expression object, and if the value we give is not a regular expression object, but a string, then search internally must first convert the passed-in string into a regular expression object, and then give a match. The match always starts at the initial position of the string, stops the match when it finds the first character that matches the regular expression, and returns the starting position of the substring that matches. If not found, return-1.

Note:search finds the first matching result, stops, and does not continue to find, so, flag G is invalid for it!

The demo is as follows:

--string.split (delimiter, limit)--

Method Split, splits the string according to the passed-in delimiter parameter, and returns a segmented array of strings.

For example:

The specified delimiter, delimiter, can be a string, or it can be a regular expression. As shown above.

Gee, isn't there a parameter limit? What is the role of it?

This optional parameter, limit, is an integer that defines the maximum length of the returned array. As follows:

--string.replace (match, replacement)--

The Replace method is used to find a string based on the rules specified by match, and to replace it with the second parameter replacement if a matching result is found. If match is a RegExp object and the global search flag g is specified, then the global lookup is replaced instead of stopping after the first match is found.

So , the parameter match can be either a RegExp object or a string, but the string is not specified as a global search flag, so if match is a string, then the lookup is stopped when the first matching substitution is found.

For example:

You think this is the end of the Replace method? No,no,no.

Remember the "clear" javascript regular expression-the "parentheses" mentioned in the previous article?

Parentheses can enclose the content that needs to be used later as a subexpression, and in a regular expression you can use a backslash \ plus a number to reference the preceding subexpression. The Replace method also has such a function, of course, if the parameter match is a RegExp object, it is quoted using the dollar symbol $ plus the corresponding number, and the number calculation rule remains unchanged.

As follows:

Another,the second parameter of replace replacement not only can be a string, also can be a function oh. This replacement callback function is called when the string matches the argument match successfully.

If I want to capitalize the first letter of all the words in a string, use the replacement function to:

Isn't it very dick.

Let's take a closer look at what the parameters of this callback function are, as follows:

The first parameter is a matching string, such as the demo above;

The second argument is a subexpression in the regular expression (parentheses), without a subexpression, without the argument

The third argument is an integer that declares where the match appears in the string;

The fourth argument is the original string

--string.match (regexp)--

The match method returns an array of results that match the regexp. The parameter regexp is a RegExp object, and if the passed argument is not a RegExp object, the match interior first converts it to a RegExp object.

Note:The match method returns the array, with the passed parameter regexp with no global search flag G, the difference is very large. The following is a description.

1, if the regexp without the flag G, then the match method according to the parameters RegExp match to the first substring, will stop to continue to look for matches, if a match is not found, return null.

As follows:

You think it's over? No,no,no.

What is powerful is that if you have a subexpression in your regular expression (parentheses), then the returned array, in Arr, also includes the results of these matching sub-expressions. Arr[0] is the matching text, Arr[1] is the first subexpression to match, and so on.

As follows:

2 . If regexp carries the global search flag G, the match method returns an array of all results that match the parameter regexp.

As follows:

exec and test methods in RegExp objects

The RegExp object defines two methods for performing pattern matching operations: Exec and test. Here's a story.

--regexp.exec (String)--

The Exec method retrieves the parameter string, which returns an array when there is a matching result. If no matching result is found, NULL is returned. The call to the Exec method is divided into two types:

1, when there is no global search flag G in RegExp, then the return result is the same as the String.match method: The returned array arr, arr[0] is the matching text, if there is a subexpression, then arr[1] ... Represent these sub-expressions. and set the lastindex to 0. Note:lastindex is a property of the RegExp object.

As follows:

2, when there is a global search flag G in RegExp, then the returned result is the same as String.match. Hey, what's the routine?

However, the difference is that it resets the properties of the RegExp object to the position of the character where the matching text was last found, instead of 0.

As follows:

What's the use of this difference?

Then you have to look at the role of lastindex. Each time a string is retrieved using the Exec or test method, they are retrieved from the location of the lastindex as a starting point.

As follows:

So, when there is a global search flag G in RegExp, it resets the lastindex to the character position that matches the text that was last found, so we can iterate over the string.

As follows:

--regexp.test (String)--

The test method detects the parameter string string, contains the matched text, and returns True if any, otherwise false. RegExp with no global search flag G is the difference between the object's property lastindex. If you do not have G, then lastindex is 0, otherwise the lastindex is set to the position of the character immediately after the last matching text was found.

As follows:

Well, the regular expression in the comb javascipt is over.

Clarify JavaScript Regular expressions

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.