且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我需要一个正则表达式转换成美国的电话号码链接

更新时间:2022-04-02 22:39:36

删除 [] 并添加 \s * 每个左右(零个或多个空白字符) \ -

Remove the [] and add \s* (zero or more whitespace characters) around each \-.

此外,你不需要逃避 - 。 (你可以拿出 \ \ -

Also, you don't need to escape the -. (You can take out the \ from \-)

说明: [ABCA-Z] 字符组的,它匹配 A b C ,或之间的任何字符 A 以Z 。结果
这不是你想要做的事。

Explanation: [abcA-Z] is a character group, which matches a, b, c, or any character between A and Z.
It's not what you're trying to do.

在应对更新的正则表达式:

In response to your updated regex:


  • 更改 [ - \.\s] [ - \.\s] + 来匹配任何这些字符中的一个或多个(例如, - 与它周围的空格>)

  • 的问题是, \b 空格和

  • 之间的边界不匹配
  • Change [-\.\s] to [-\.\s]+ to match one or more of any of those characters (eg, a - with spaces around it)
  • The problem is that \b doesn't match the boundary between a space and a (.