Match only if not preceded or followed by a digit

后端 未结 1 777
不思量自难忘°
不思量自难忘° 2021-01-24 09:10

I have this regular expression to look for phone numbers in a codebase:

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

How can I modify this so th

相关标签:
1条回答
  • 2021-01-24 09:49

    Lookahead and lookbehind will help you with these restrictions - if they are supported, like

    /(?<!\d)\d{3}[.\s-]?\d{3}[.\s-]?\d{4}(?!\d)/g
    

    You can replace the lookbehind (?<!\d) with (?:^|\D) if it is not supported. You can replace the lookahead (?!\d) with (?:\D|$) if it is not supported.

    You can find a demo here: https://regex101.com/r/aS8jQ8/1

    0 讨论(0)
提交回复
热议问题