首页 > 解决方案 > 有效网页 url 和 html 电话 url 的正则表达式

问题描述

**https://www.test.com**

I have written regex for the above url like below
/^\{[\$\w][\$\w\.\[\]]+\}|^https{0,1}:\/\/[\w\-\.:\/\$\{\}=\?&#\~]+$/

now i am trying to add **tel:15556665872** condition to the above regex like below

/^\{[\$\w][\$\w\.\[\]]+\} |^tel{0,1}:[\d]+ |^https{0,1}:\/\/[\w\-\.:\/\$\{\}=\?&#\~]+$/

上面的正则表达式应该验证 web url 和 tel url,但它只验证 web url ( https://www.test.com ),而不是验证 (tel:23423234)

有人可以帮忙吗?

标签: javascripthtmlregex

解决方案


如果您知道在 href 中永远不会有“<”字符,您可以这样做:

/<a\s+href=["']([^>]+)['"]/i

如果您需要解释,请询问。我建议使用这个站点来帮助您编写正则表达式字符串。

PS 如果您要匹配的字符串在一行上,请仅使用 ^ 和 $。我上面的正则表达式适用于类似的例子,<div><a href="tel:15556665872">Click to Call</a></div>但如果我将我的正则表达式字符串包装在 ^$ 中,它就不会。


推荐阅读