In JavaScript, using EXEC to perform a regular expression global matching walkthrough

Source: Internet
Author: User
Tags modifier regular expression

Let's take a look at common usage:
:

The code is as follows Copy Code
<script type= "Text/javascript" >
var pattern =/http://([^/s]+)/;
Alert (pattern.exec (' http://www.codebit.cn ')); http://www.codebit.cn,www.codebit.cn
Alert (pattern.exec (' http://YITU.org ')); http://YITU.org,YITU.org
Can also be written directly/http://([^/]+)/.exec (' http://www.codebit.cn ');
</script>

Next, look at the bizarre events in the global model:
:

The code is as follows Copy Code
<script type= "Text/javascript" >
var pattern =/http://([^/s]+)/g; Using the G modifier
Alert (pattern.exec (' http://www.codebit.cn ')); http://www.codebit.cn,www.codebit.cn
Alert (pattern.exec (' http://YITU.org ')); does not return the desired http://YITU.org,YITU.org, but instead returns a null
</script>

The second statement does not return the desired result, but instead returns NULL because:
In global mode, when exec () finds the text that matches the expression, after the match, it sets the Lastindex property of the regular expression object to the next position in the last character of 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.
The following are the ways to match the normal global mode:
:

The code is as follows Copy Code
<script type= "Text/javascript" >
var pattern =/http://([^/s]+)/g;
var str = "codebit.cn:http://www.codebit.cn | Yitu.org:http://yitu.org ";
var result;
while (result = Pattern.exec (str))!= null) {
Alert ("Result:" + result + "lastindex:" + pattern.lastindex);
}
result:http://www.codebit.cn,www.codebit.cn lastindex:34
Result:http://yitu.org,yitu.org lastindex:67
</script>

From the code above, we can see that the problem in the second code, the impact factor is lastindex, so we can solve the problem by manually placing lastindex 0.
:

The code is as follows Copy Code
<script type= "Text/javascript" >
var pattern =/http://([^/s]+)/g; Using the G modifier
Alert (pattern.exec (' http://www.codebit.cn ')); http://www.codebit.cn,www.codebit.cn
Pattern.lastindex = 0;
Alert (pattern.exec (' http://YITU.org ')); http://YITU.org,YITU.org
</script>

Summarize:
In global mode, if you want to start retrieving a new string after a pattern match is completed in a string, you must manually reset the Lastindex property to 0

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.