【发布时间】:2016-11-30 08:25:24
【问题描述】:
我需要从字符串中匹配域名。使用三种不同的模式。
var str=" with http match http://www.some.com and normal website type some.com and with www.some.com ";
var url = /(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/g;
console.log(str.match(url))
sn-p 以上只匹配http://www.some.com。
但我需要匹配三种类型。
http://www.some.comwww.some.comsome.com
帮我找到结果。我的正则表达式不太好。我从堆栈溢出中得到这个正则表达式模式。但不满足三个条件。
【问题讨论】:
-
只需将
(...)分组并使用可选运算符? -
看我的回答,也有演示
标签: javascript regex match