繁体   English   中英

尝试使用正则表达式匹配URL

[英]Trying to match URL using regex

我正在尝试使用正则表达式匹配URL

https?:\/\/.*\..*

但是,如果URL后面出现空格,则无法理解如何结束比赛。

例如在下面的图像中,对于最后一场比赛,我希望它在空格之前结束。 但是似乎没有任何作用。

您能否解释一下为什么在末尾添加\\ b(单词边界)无效吗?

在此处输入图片说明

只需使用\\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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM