【问题标题】:How can i detect any type of url with regular expressions [closed]我如何使用正则表达式检测任何类型的 url [关闭]
【发布时间】:2014-07-09 06:49:37
【问题描述】:

是否有任何正则表达式来检测完整的 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?):\/\/($|[^ ]+)/;

在这个问题Autolink URL in ContentEditable Iframe 我正在尝试修复 regx

请帮忙

谢谢

【问题讨论】:

  • 您的实际问题是什么?
  • 你看我的问题了吗? @AvinashRaj
  • 你的问题不清楚。你想达到什么目标?
  • 当我在文本框中键入 http:// 时,由于我正在使用的 regx,http:// 也被检测到,是否有任何 regx 可以检测到完整的 url

标签: javascript jquery regex contenteditable


【解决方案1】:

更新版本 - 测试

^((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

【讨论】:

  • 检查这个小提琴这对我的情况有用regex101.com/r/nT7gA2/2
  • hay @hex494D49 你能帮我获取 www.xxx.com 或喜欢 www.google.co.in
  • @dhee 试试更新版本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-01
  • 2013-11-08
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
相关资源
最近更新 更多