javascript纯文本url解析
我正在尝试搜索以 http 开头的普通旧字符串,但我找到的所有正则表达式似乎都不适用于 javascript,我似乎也无法在 javascript 中找到这样的示例。
var test = /\b(?:(?:https?|ftp|file)://www\.|ftp\.)[-A-Z0-9+&@#/%=~_|$?!:,.]*[A-Z0-9+&@#/%=~_|$]/;
但是当我尝试运行它时,我收到“意外令牌 |”错误。
I'm trying to search plain old strings for urls that begin with http, but all the regex I find doesn't seem to work in javascript nor can I seem to find an example of this in javascript.
This is the one I'm trying to use from here and here:
var test = /\b(?:(?:https?|ftp|file)://www\.|ftp\.)[-A-Z0-9+&@#/%=~_|$?!:,.]*[A-Z0-9+&@#/%=~_|$]/;
But when I try to run it, I get "Unexpected token |" errors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,评论似乎不够,很难找到完整的答案。我重写了整个正确的正则表达式:(经过测试,效果很好)
末尾的
i
表示“忽略大小写”,因此对于此正则表达式来说这是必要的。Ok, a comment seems to be not enough, hard to find full answer. I rewrite whole proper regexp: (tested, it works good)
The
i
on the end means 'ignore case', so it is necessary for this regexp.您使用
/
作为正则表达式分隔符,并且还在正则表达式中(在 www 之前)使用/
,因此正则表达式实际上在第一个 / www 之前终止。将其更改为:You're using
/
as your regex delimiter, and are also using/
within the regex (before www), so the regex actually terminates after the first / before www. Change it to: