Javascript URL 正则表达式问题
我使用这个正则表达式来表示 URL:
var re = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;
但是,它似乎只匹配单独的 URL。如果您将 URL 添加到已键入的消息中,它将不会匹配:
Test message: www.google.com
不会匹配。无论 URL 中包含什么内容,如何使其与 URL 匹配?
I use this regex for URL's:
var re = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;
However, it seems to only be matching URL's that are alone. If you add a URL to an already typed message, it won't match:
Test message: www.google.com
Won't match. How do I make it match URL's no matter what's included with them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为表达式开头的
^
字符意味着“匹配字符串的开头”(换句话说,您拥有的是 starts-with 搜索)。删除它,它将匹配测试字符串中的任何位置。It's because the
^
character at the beginning of the expression means "match the beginning of the string" (in other words, what you have is a starts-with search). Remove that and it will match anywhere in the test string.这只是因为您使用了插入符号 (
^
)。这意味着“仅匹配输入的开头”。删除它,你应该得到你想要的。That's simply because you're using the caret symbol (
^
). That means "only match the start of input." Remove that and you should get what you want.