How to Replace Plain Urls With Links

How to replace plain URLs with links?

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.

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'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'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.

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.

How to replace plain URLs with links, with example?

Over at CodeReview this question was answered quite splendidly.

function replaceURLWithHTMLLinks(text) {
var re = /(\(.*?)?\b((?:https?|ftp|file):\/\/[-a-z0-9+&@#\/%?=~_()|!:,.;]*[-a-z0-9+&@#\/%=~_()|])/ig;
return text.replace(re, function(match, lParens, url) {
var rParens = '';
lParens = lParens || '';

// Try to strip the same number of right parens from url
// as there are left parens. Here, lParenCounter must be
// a RegExp object. You cannot use a literal
// while (/\(/g.exec(lParens)) { ... }
// because an object is needed to store the lastIndex state.
var lParenCounter = /\(/g;
while (lParenCounter.exec(lParens)) {
var m;
// We want m[1] to be greedy, unless a period precedes the
// right parenthesis. These tests cannot be simplified as
// /(.*)(\.?\).*)/.exec(url)
// because if (.*) is greedy then \.? never gets a chance.
if (m = /(.*)(\.\).*)/.exec(url) ||
/(.*)(\).*)/.exec(url)) {
url = m[1];
rParens = m[2] + rParens;
}
}
return lParens + "<a href='" + url + "'>" + url + "</a>" + rParens;
});
}

Note: I had errors with the "@" symbol in the "var re" - I just replaced it with @@

How to replace plain text URLs with links and hashtags in PHP

Your regex for hashtag is this:

/[#]+([A-Za-z0-9-_]+)/

Your stated goal is to make sure it's not part of a URL, which you identify by:

/https?\:\/\//

You can try to use a negative look-behind:

/(?<!https?\:\/\/)[^#]*[#]+([A-Za-z0-9-_]+)

This is not enough for all general cases, but it sounds like you're trying to solve a problem within a scope under your control (a text file you own or something) so hopefully this well help you.

Replace all URLs in text to clickable links in PHP

function convert($input) {
$pattern = '@(http(s)?://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])@';
return $output = preg_replace($pattern, '<a href="http$2://$3">$0</a>', $input);
}

demo

Replace plain URL with link if domain matches

You could use regex to replace and then append.

var str = "Hey this is a link to somesite.co bye bye";
str = str.replace(/(somesite\.co)/g, "<a href="$1">$1</a>");
var a = document.createElement('a');
a.outerHTML = str;
document.body.appendChild(a);


Related Topics



Leave a reply



Submit