是否有任何正则表达式来检测完整的网址
在我的代码中,我使用此 regx urlRegex 来查找我的内容可编辑 iframe 文本中的任何 url
urlRegex = /http(s?):\/\/($|[^ ]+)/
当我输入 http:// 时,我遇到问题,这会检测到,但不会检测到 www.google.com
我必须检测网址,例如
http://www.google.com
https://www.google.com
www.google.com
http://localhost:51932/default.aspx
我的代码是
var iframe = Iframe,
textbox = iframe.contents().find("body")[0],
urlRegex = /http(s?):\/\/($|[^ ]+)/;
在这个问题中Autolink URL in ContentEditable Iframe我正在尝试修复 regx
请帮忙
谢谢
最佳答案
更新版本 - 已测试
^((https?|s?ftp):\/\/)?(((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])))(:[1-9][0-9]+)?(\/)?([/?].+)?$
只有两次失败(目前:))
http://localhost:51932/default.aspx
https://192.168.0.25:5132/default.aspx
www.google.com
http://www.google.com
mailto:somebody@google.com // fail
somebody@google.com // fail
www.url-with-querystring.com/?url=has-querystring
http://www.xxx.events
sftp://ftp.something.com
www.google.co.in
www.xxx.com
关于javascript - 如何使用正则表达式检测任何类型的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24647038/