我正在尝试创建一个提取 URL 的表达式

发布于 2024-09-09 20:16:39 字数 218 浏览 3 评论 0原文

我想从网页中提取 URL,这些 URL 本身只是 URL,而不是超链接等,它们只是文本。一些示例是 http://www.example.comhttp://example.comwww.example.com 等。我对正则表达式非常陌生,所以我在网上复制并粘贴了 20 个表达式,但都失败了。我不知道我这样做是否正确。任何帮助将不胜感激。

I want to extract URLs from a webpage these are just URLs by themselves not hyperlinks etc., they are just text. Some examples would be http://www.example.com, http://example.com, www.example.com etc. I am extremely new at regex so I have copy and pasted like 20 expressions online all failed to work. I don't know if I am doing it right or not. Any help would be really appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

就是爱搞怪 2024-09-16 20:16:39

我写了一篇关于使用 Regex 来定位 HTML 页面中的链接的文章(目的是使用 JavaScript 在弹出窗口中打开外部链接或指向 PDF 等文档的链接)。

最终的正则表达式是:
^(?:[./]+)?(?:Assets|https?://(?!(?:www.)?integralist))

完整帖子在这里:
http://www .integralist.co.uk/javascript/regular-expression-to-open-external-links-in-popup-window/

该解决方案并不完美,但可能会帮助您指明正确的方向。

标记

I wrote a post on using Regex to locate links within a HTML page (the intent was to use JavaScript to open external links or links to documents such as PDF's etc in a popup window).

The final regex was:
^(?:[./]+)?(?:Assets|https?://(?!(?:www.)?integralist))

The full post is here:
http://www.integralist.co.uk/javascript/regular-expression-to-open-external-links-in-popup-window/

The solution wont be perfect but might help point you in the right direction.

Mark

三寸金莲 2024-09-16 20:16:39

您可能没有转义您的 .。每一个都需要使用 \.

请访问 strfriend.com。它有一个 URL 示例,并以图形方式表示。

它建议的示例是:

^((ht|f)tp(s?)://|~/|/)?(\w+:\w+@)?([a-zA-Z]{1}([\w-]+.)+(\w{2,5}))(:\d{1,5})?((/?\w+/)+|/?)(\w+.\w{3,4})?((\?\w+=\w+)?(&\w+=\w+)*)?

You're probably not escaping your .s. You need to use \. for each one.

Take a look at strfriend.com. It has a URL example, and represents it graphically.

The example it suggests is:

^((ht|f)tp(s?)://|~/|/)?(\w+:\w+@)?([a-zA-Z]{1}([\w-]+.)+(\w{2,5}))(:\d{1,5})?((/?\w+/)+|/?)(\w+.\w{3,4})?((\?\w+=\w+)?(&\w+=\w+)*)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文