javascript - 正则表达式匹配长字符串中的 URL

标签 javascript c# regex

我这里有一个字符串:

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/

相关文章:

javascript - 隐藏 <select> 元素中的选定值?

javascript - 获取 puppeteer 页面的默认超时设置

c# - 在 Winforms 中处理 Gmail(发送/接收)

c# - TabItem/TabControl 中的 DataGridTextColumn 标题绑定(bind)问题

c# - 单元测试,如何模拟空数据的返回以获得 100% 的覆盖率

python正则表达式匹配 "ab"或 "ba"单词

javascript - 为什么 Safari 在我刷新时会执行两次 fetch()?

javascript - 如何通过 JavaScript 函数创建 Angular JS Material 元素?

regex - 如何检查文件名是否与通配符模式匹配

java - 当相同的正则表达式在 shell 中工作时,Java 中的正则表达式不起作用