我希望能够匹配下面的例子:
www.example.com
http://example.com
https://example.com
我有以下正则表达式,它不匹配 www.
但会匹配 http://
https://
。我需要匹配上面示例中的任何前缀,直到下一个空格,即整个 URL。
var regx = ((\s))(http?:\/\/)|(https?:\/\/)|(www\.)(?=\s{1});
假设我有一个如下所示的字符串:
我从 www.stackoverflow.com 和那里的人那里找到了很多帮助!
我想在那个字符串上运行匹配并得到
www.stackoverflow.com
谢谢!
最佳答案
你可以试试
(?:www|https?)[^\s]+
这里是 online demo
示例代码:
var str="I have found a lot of help off www.stackoverflow.com and the people on there!";
var found=str.match(/(?:www|https?)[^\s]+/gi);
alert(found);
图案解释:
(?: group, but do not capture:
www 'www'
| OR
http 'http'
s? 's' (optional)
) end of grouping
[^\s]+ any character except: whitespace
(\n, \r, \t, \f, and " ") (1 or more times)
关于javascript - 正则表达式匹配字符串直到空格 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25249535/