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!