Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. funkatron revised this gist Feb 23, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Replace URIs with anchor tags in a string
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    function hrefs2links(text) {
    // Uses Grubers regex with some extra camptures
    var urire = /\b(?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|(www\d{0,3}[.]))((?:[^\s()<>]+|\([^\s()<>]+\))+(?:\([^\s()<>]+\)|[^`!()\[\]{};:'".,<>?«»“”‘’\s]))/g;
    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>');
  2. @anttih anttih revised this gist Feb 4, 2010. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Replace URIs with anchor tags in a string
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    function hrefs2links(text) {
    // Uses Grubers regex with some extra camptures
    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
  3. @anttih anttih created this gist Feb 4, 2010.
    10 changes: 10 additions & 0 deletions Replace URIs with anchor tags in a string
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    function hrefs2links(text) {
    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>');
    }

    var text = hrefs2links("Check out http://example.com");

    // text is 'Check out <a href="http://example.com">example.com</a>'