Summary of the regexp exec function inside JavaScript

Source: Internet
Author: User

In our front-end, regular expressions are often used to retrieve strings, just as JavaScript provides regexp to support regular expressions, while the main method of RegExp objects is exec ().  

Grammar

Regexpobject.exec (String)
Parameters Describe
String Necessary. The string to retrieve.

return value:

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

Description

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

If EXEC () finds the matched text, it returns an array of results. Otherwise, NULL is returned. The No. 0 element of this array is the text that matches the regular expression, and the 1th element is the text that matches the 1th sub-expression of regexpobject (if any), and the 2nd element is the text (if any) that matches the 2nd sub-expression of regexpobject, in such Push. In addition to the array element and the length property, the Exec () method returns two properties. The Index property declares the position of the first character of the matched text. The input property holds the string that was 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 behavior of exec () is slightly more complex. It will begin retrieving string strings at the character specified by the LastIndex property of Regexpobject. When exec () finds text that matches the expression, it sets the Regexpobject LastIndex property to the next position of the last character of the matched text after the match. This means that you can iterate through all the matching text in a string by calling the Exec () method repeatedly. When exec () can no longer find a matching text, it returns null and resets the LastIndex property to 0.

Now we're going to focus on the third paragraph,

Look at an example:

 

The results of the operation are as follows:

W3school
14
W3school
24
0

You can see that in the global mode lastindex will continue to increase until all the characters of the string are traversed, and finally become 0.

So one of the important notes is that

If you want to start retrieving a new string after you have completed a pattern match in a string, you must manually reset the LastIndex property to 0

Examples are as follows:

var str = "Visit W3school, W3school is a place to study web technology.";  var Patt = new RegExp ("W3school", "G"); var result;  Result=patt.exec (str);  document.write (result);  document.write ("<br/>");  document.write (Patt.lastindex);  document.write ("<br/>"), var aa= "W3school sfd SFSF" var Ddresult;  ddresult= patt.exec (AA); document.write (Ddresult);

The results are as follows:

W3school
14
Null

The result of the last output is null because the lastindex of the first match result becomes 14. To retrieve the second string will start from the position of 14, the front 13 characters to jump past, so we can not find, we want to look at it from the beginning, only need to retrieve the start of the second string, let its lastindex set to 0

The result would be:

W3school
14
W3school

But this is only the overall problem, local words, without this problem, you can take the above example of the global removal, you can get

var str = "Visit W3school, W3school is a place to study web technology.";  var Patt = new RegExp ("W3school", ""); var result;  Result=patt.exec (str);  document.write (result);  document.write ("<br/>");  document.write (Patt.lastindex);  document.write ("<br/>"), var aa= "W3school sfd SFSF" var Ddresult;  ddresult= patt.exec (AA); document.write (Ddresult);

The results of the operation are as follows:

W3school
0
W3school

Summary of the regexp exec function inside JavaScript

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.