Js Regular Expression $1 $2 $3 $4 $5 $6 $7 $8 $9 attribute, return the sub-matching result

Source: Internet
Author: User

Function: $1-$9 stores the matching results of the last nine regular expressions in the regular expression. These results are arranged in sequence according to the occurrence order of the submatching.

Basic syntax RegExp. $ n

Note: These attributes are static. Except for the second parameter in replace, You can omit RegExp. You must add RegExp to all other attributes.

Case study: demo1Copy codeThe Code is as follows: <Script language = "javascript" type = "text/javascript">
// Create a matched string
Var objStr = "this is my mobile phone number 13100000000"
// Create a regular expression to match the mobile phone number
Var re =/(13) (/d {8})/; // This regular expression can match the mobile phone number starting with 11 and start () indicates a child match.
Document. write (objStr. replace (re, "$1 $2 *********"); // replace the content of the string according to the regular expression in the Privacy Field
// If the second child matches the result, that is, if the third digit in the mobile phone number is less than or equal to 3, the mobile phone number is the UNICOM number, otherwise it is the number of another carrier
If (RegExp. $2 <= 3 ){
Document. write ("this is the China Unicom mobile phone number ");
} Else {
Document. write ("this is a mobile or Telecom phone number ");
}
</Script>
</Html>

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

Demo2

Copy codeThe Code is as follows: <Script language = "javascript" type = "text/javascript">
// Create a string to be matched
Var objStr = "this is my mobile phone number 13112345678, this is my friend's mobile phone number 13912345678 ";
// Create a regular expression object that matches the mobile phone number, which is implicitly created
Var reg =/(13) (/d {8})/g; // g is a global matching parameter that matches 11 mobile phone numbers;
// Match the string. If the matching result is met, the returned result is put into the array.
Var arr = objStr. match (reg );
With (document ){
If (arr! = Null) {// If a matching result exists
Write ("detected" + arr. length + "mobile phone number"); // display the number of matched phone numbers
// Output the results cyclically and determine the carrier
For (var I = 0; I <arr. length; I ++ ){
// Perform a secondary match on the matched phone number to obtain the third number.
Arr [I] = arr [I]. toString (). replace (reg, "$1 $2 ********");
// Output the phone numbers in the form of a list
Write ("<li>" + arr [I]);
// If the third phone number is less than or equal to 3, the phone number is the UNICOM phone number. Otherwise, the phone number is the number of another carrier.
If (RegExp. $2 <= 3 ){
Write ("this is a China Unicom mobile phone number! ");
} Else {
Write ("this is a mobile or Telecom phone number! ");
}
} // End of the for Loop
} // End of if
} // End of
</Script>
</Html>

I hope you can practice more and leave a message if you have any questions. I will do my best to help you solve them!

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.