[英]Trying to match URL using regex
只需使用\\S
:
https?:\/\/.*\.\S*
\\S
表示:匹配所有非空格字符(空格,制表符,delim ..)
查看下面的解决方案,对最后一个空格使用惰性和非捕获组:
在此处查找更好的正则表达式' 检查字符串是否为有效URL的最佳正则表达式是什么?
//well let us dive into this: var matches = document.querySelector("pre").textContent.match(/https?:\\/\\/.*\\..*/g); console.log(matches); /* your regex does the following search for http:// or https:// then you want to search for every character that is not a newline until you find a dot after that you simply search for everything that is not a newline. you need lazy and a non-capturing group, lazy is ? - (?=\\s) */ var matches2 = document.querySelector("pre").textContent.match(/https?:\\/\\/.+?\\..+?(?=\\s)/g); console.log(matches2);
<pre> foo@demo.net http://foo.co.uk/ http://regexr.com/foo.html?q=bard https://mediatemple.net jhjhjhjhjhjh </pre>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.