如何用链接替换普通URL? [英] How to replace plain URLs with links?
问题描述
我正在使用下面的函数来匹配给定文本中的URL,并将其替换为HTML链接。正则表达式工作得很好,但目前我只替换第一场比赛。
I am using the function below to match URLs inside a given text and replace them for HTML links. The regular expression is working great, but currently I am only replacing the first match.
如何替换所有的URL?我想我应该使用 exec 命令,但我并没有真正想办法。
How I can replace all the URL? I guess I should be using the exec command, but I did not really figure how to do it.
function replaceURLWithHTMLLinks(text) {
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i;
return text.replace(exp,"<a href='$1'>$1</a>");
}
推荐答案
首先,滚动自己用于解析URL的regexp是一个可怕的想法。您必须想象这是一个常见的问题,有人编写,调试并测试根据 RFC ,它的库。 URI很复杂 - 请查看网址代码解析Node.js 和 URI方案上的维基百科页面。
First off, rolling your own regexp to parse URLs is a terrible idea. You must imagine this is a common enough problem that someone has written, debugged and tested a library for it, according to the RFCs. URIs are complex - check out the code for URL parsing in Node.js and the Wikipedia page on URI schemes.
解析网址时有很多边缘情况:国际域名,实际( .museum
)与不存在( .etc
) TLD,奇怪的标点符号,包括括号,标点符号在URL的结尾,IPV6主机名等。
There are a ton of edge cases when it comes to parsing URLs: international domain names, actual (.museum
) vs. nonexistent (.etc
) TLDs, weird punctuation including parentheses, punctuation at the end of the URL, IPV6 hostnames etc.
I've looked at a ton of libraries, and there are a few worth using despite some downsides:
- Soapbox的 linkify 已经付出了一些认真的努力,和 2015年6月的主要重构 删除了jQuery依赖项。它仍然有 IDN问题。
- < a href =http://alexcorvi.github.io/anchorme.js/ =noreferrer> AnchorMe 是声称要更快和更精简。一些 IDN问题也是如此。
- Autolinker.js 非常具体地列出了功能(例如将正确处理HTML输入。该实用程序不会更改anchor()标记中的
href
属性。当演示可用时,我会对它进行一些测试。
- Soapbox's linkify has seen some serious effort put into it, and a major refactor in June 2015 removed the jQuery dependency. It still has issues with IDNs.
- AnchorMe is a newcomer that claims to be faster and leaner. Some IDN issues as well.
- Autolinker.js lists features very specifically (e.g. "Will properly handle HTML input. The utility will not change the
href
attribute inside anchor () tags"). I'll thrown some tests at it when a demo becomes available.
我为此任务快速取消资格的图书馆:
Libraries that I've disqualified quickly for this task:
- Django的urlize 没有正确处理某些TLD (这是官方有效顶级域名列表。没有演示。
- autolink-js 在没有http://的情况下不会检测到www.google.com,所以它不太适合自动链接偶然URL(没有方案/协议)以纯文本形式找到。
- Ben Alman的链接并不是马从2009年开始。
- Django's urlize didn't handle certain TLDs properly (here is the official list of valid TLDs. No demo.
- autolink-js wouldn't detect "www.google.com" without http://, so it's not quite suitable for autolinking "casual URLs" (without a scheme/protocol) found in plain text.
- Ben Alman's linkify hasn't been maintained since 2009.
如果你坚持使用正则表达式,最全面的是来自组件的URL正则表达式,但它会通过查看错误地检测到一些不存在的双字母TLD。
If you insist on a regular expression, the most comprehensive is the URL regexp from Component, though it will falsely detect some non-existent two-letter TLDs by looking at it.
这篇关于如何用链接替换普通URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!