Use Regex to find a phone number on a page not in an anchor

前端 未结 2 1723
长情又很酷
长情又很酷 2020-12-20 00:41

I have this regex expression that searches for a phone number pattern:

[(]?\\d{3}[)]?[(\\s)?.-]\\d{3}[\\s.-]\\d{4}

This matches phone numbe

2条回答
  •  臣服心动
    2020-12-20 00:57

    Use this as your regex:

    (.*?([(]?(\d{3})[)]?[(\s)?.-](\d{3})[\s.-](\d{4})).*?<\/a>)|([(]?(\d{3})[)]?[(\s)?.-](\d{3})[\s.-](\d{4}))
    

    Use this as your replace string:

    ($3$7) $4$8-$5$9
    

    This finds all phone numbers, both outside and inside of href tags, however, in all cases it returns the phone number itself as specific regex groups. Therefore, you can enclose each phone number found inside new href tags, because, where they exist, you are replacing the original href tags.

    A regex group or "capture group" captures a specific part of what matched the overall regex expression. They are created by enclosing part of the regex in parenthesis. These groups are numbered from left to right by order of their opening parenthesis and the part of the input they match can be reference by placing a $ in front of that number in Javascript. Other implementations use \ for this purpose. This is called a back reference. Back references can appear later in your regex expression, or in your replacement string (as done earlier in this answer). More information: http://www.regular-expressions.info/backref.html

    To use a simpler example, suppose you had a document containing account numbers and other information. Each account number is proceeded by the word "account", which you want to change to "acct", but "account" appears elsewhere in the document so you cannot simply do a find and replace on it alone. You could use a regex of account ([0-9]+). In this regex, ([0-9]+) forms a group which will match the actual account number, which we can back reference as $1 in our replacement string, which becomes acct $1.

    You can test this out here: http://regexr.com/

提交回复
热议问题