Javascript 匹配字符串中多次出现的正则表达式
是否可以在字符串中匹配正则表达式的多次出现?例如,我想知道我的字符串是否包含多个url,我想得到一个可用的结果,如数组:Javascript 匹配字符串中多次出现的正则表达式,javascript,regex,Javascript,Regex,是否可以在字符串中匹配正则表达式的多次出现?例如,我想知道我的字符串是否包含多个url,我想得到一个可用的结果,如数组: "hey check out http://www.example.com and www.url.io".match(new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?([^ ])+")) 将返回: ["http://
"hey check out http://www.example.com and www.url.io".match(new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?([^ ])+"))
将返回:
["http://www.example.com","www.url.io"]
console.log(“嘿,退房http://www.example.com 和www.url.io.match(新的RegExp(([a-zA-Z0-9]+:/)([a-zA-Z0-9+:[a-zA-Z0-9+]))([a-zA-Z0-9.-]+\.[a-zA-z]{2,4})(:[0-9]+)([^]))+)
您可以尝试以下正则表达式:
/(http?[^\s]+)|(www?[^\s]+)/g
演示:
函数urlify(文本){
var urlRegex=/(http?[^\s]+)|(www?[^\s]+)/g;
返回text.match(urlRegex);
}
log(urlify)(“嘿,签出http://www.example.com 及www.url.io);代码>