我这里有一个字符串:
Good Day,I would like to $1000 (for example remove these parenthesis) $test $ asdf [and remove these brackets] inquire the price of the 3D product name here. Currently we have the other product name here which I believe has an accuracy of about 0.0005 199 200. http://www.example.com this is a test
我在这里链接了一个正则表达式:https://regex101.com/r/yE4kW6/7
其中有这个正则表达式:
[^\w\s|https?:\/\/.\-*\s]|\W*\b\d+(?:\.\d+)?\b
我现在想做的是让它匹配整个 URL。看来我的 |https?:\/\/.\-*\s
正在找到网址,但它忽略了它,可能是因为 ^
开头该集?我可以使用一些帮助让它匹配字符串中的 URL。
最佳答案
除了我的评论之外,您还可以想出一个正则表达式,例如:
~(https?://\S+)~
# start / end with the tilde ~ as delimiters
# look for http/https, followed by ://
# match anything that is not a whitespace
# capture everything into a group
对于 JavaScript,这可以归结为(注意转义的正斜杠):
(https?:\/\/\S+)
这不是很具体,并且在很大程度上取决于您的输入字符串(也可以匹配 URL 的无效字符)。 请参阅a demo on regex101 here 。
关于javascript - 正则表达式匹配长字符串中的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34904560/