The compile method of RegExp object of JS regular expression compile regular expression _ regular expression

Source: Internet
Author: User
Function Description: This method can compile the specified regular expression, the execution speed of the regular expression after compilation increases, and if the regular expression is invoked multiple times, calling the compile method can effectively improve the execution speed of the code, and if the regular expression can only be used once, it will not have a significant effect.

Basic syntax: Objreg.compile (Pattern[,flag])
Objreg required, regexp the name of the object variable
Pattern must option regular expression
Flag Options Matching Options

Copy Code code as follows:

<script language= "javascript" type= "Text/javascript" >
The string to match
var objstr= "My mobile number 13522222222, his mobile number 13233333333 her mobile number 13988888888";
Set the regular expression to match 13 of the first 11 for the mobile phone number, global match (here is the matching mobile phone number)
var reg=new RegExp ("13[4-9] (//d) {8}", "G");
Prompts the user to find the mobile number and then prints out the result
document.write ("Find mobile phone number");
Call function to print matching results
Findphonenumbers (Objstr.match (reg));
Recompile regular expression
Reg.compile ("13[0-3] (//d) {8}", "G");
document.write ("Find Unicom mobile phone number");
Findphonenumbers (Objstr.match (reg));
Define output return result function
function Findphonenumbers (arr) {
Using circular output data
for (Var i=0;i<arr.length;i++) {
document.write ("<li>" +arr[i]+ "<br>");
}
}
</script>


Using the Compile method, you can also modify and recompile the specified regular expression to improve the adaptability of regular expressions!

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.