JS Regular Expression $1$2$3$4$5$6$7$8$9 property, returns the result of a child match _ regular expression

Source: Internet
Author: User
Function: $1-$9 holds the matching results of the most recent 9 regular expressions in the regular expression, which are listed in the order in which the child matches appear.

Basic syntax RegExp. $n

Note: These properties are static, except that the second parameter in replace can omit RegExp, and other places should be added with RegExp.

Case Explanation: Demo1
Copy Code code as follows:

<script language= "javascript" type= "Text/javascript" >
To create a string to be matched
var objstr= "This is my mobile number 13100000000"
Create regular expressions to match your phone number
var re=/(d) (/d{8})/;//the regular expression can match a cell phone number with a number starting with 13 11 for the sign () to match the child
document.write (Objstr.replace (Re, "$1$2********");/in privacy Replace the string with the contents of the regular expression
If the second child matches the result, that is, the third digit number in the cell phone number is less than or equal to 3, the mobile number is the Unicom number, otherwise the number of the other operator
if (regexp.$2<=3) {
document.write ("This is Unicom mobile phone number");
}else{
document.write ("It is mobile or mobile phone number");
}
</script>

But the value of $n is changed once without a match, so we need to match this when we match multiple phone numbers in a string.

Case explanation Demo2

Copy Code code as follows:

<script language= "javascript" type= "Text/javascript" >
Create a string that needs to be matched
var objstr= "This is my mobile number 13112345678, this is my friend's mobile number 13912345678";
Creates a regular expression object that matches the phone number, implicitly creating
The Var reg=/(d) (/d{8})/g;//g is a global matching parameter, matching 11-bit phone numbers;
Matches a string if it matches the result, the returned result is placed in an array
var arr=objstr.match (REG);
With (document) {
if (arr!=null) {//If the match has a result
Write ("Detected" +arr.length+ "mobile number");//show how many phone numbers to match
Loop output The results and judge the operator
for (Var i=0;i<arr.length;i++) {
Match the phone number two times, get the third number of numbers
Arr[i]=arr[i].tostring (). Replace (Reg, "$1$2********");
Output individual phone numbers as a list
Write ("<li>" +arr[i]);
If the third telephone number is less than or equal to 3, the telephone number is the Unicom number, otherwise the number of the other operator
if (regexp.$2<=3) {
Write ("This is a unicom mobile phone number!") ");
}else{
Write ("This is a mobile or telecom phone number!") ");
}
The end of the}//for cycle
The end of the}//if
The end of the}//with
</script>

I hope you have a lot of practice, what questions you can leave a message, I will do my best to help you solve!

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.