我如何用正则表达式检测任何类型的url

How can i detect any type of url with regular expressions

本文关键字:类型 url 任何 检测 何用 正则表达式      更新时间:2023-09-26

是否有任何regx来检测完整的url

在我的代码中,我使用这个regx urlRegex来找出我的内容可编辑iframe的文本中的任何url

urlRegex = /http(s?):'/'/($|[^ ]+)/

我得到问题当我输入http://这得到检测,这是不检测www.google.com

我必须检测像

这样的url
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?):'/'/($|[^ ]+)/;

在这个问题自动链接URL在contentteditable 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