Skip to content

Instantly share code, notes, and snippets.

/*jslint regexp: false*/
"use strict";
function hrefs2links(text) {
// Uses Grubers regex with some extra captures
var urire = /\b(?:[a-z][\w\-]+:(?:\/{1,3}|[a-z0-9%])|(www\d{0,3}[.]))((?:[^\s()<>]+|\([^\s()<>]+\))+(?:\([^\s()<>]+\)|[^`!()\[\]{};:'".,<>?«»“”‘’\s]))/g;
// replace URIs with HTML anchors
return text.replace(urire, '<a href="$&">$1$2</a>');
}
(?xi)
\b
( # Capture 1: entire matched URL
(?:
[a-z][\w-]+: # URL protocol and colon
(?:
/{1,3} # 1-3 slashes
| # or
[a-z0-9%] # Single letter or digit or '%'
# (Trying not to match e.g. "URI::Escape")