String linkify - Node.js HTML

Node.js examples for HTML:Element

Description

String linkify

Demo Code

String.prototype.linkify = function(target) {
  var str = String(this);

  str = str.stripTags();//  w w  w  .  j av  a  2 s .  com

  str = str.replace(/\b[a-z0-9-_!#$%&'`=\*\+\-\/\?\^\{\|\}\~]+@[a-z0-9]+?[a-z0-9\-\.]+\.[a-z]{2,4}/ig, function() {
    return '<a href="mailto:' + arguments[0] + '">' + arguments[0] + '</a>';
  });

  str = str.replace(/(^|[^a-z0-9@\.\-]{1}|[a-zA-Z]+:\/\/|www\.)((?:[0-9]{1,3}\.){3}[0-9]{1,3}|(?:[a-z0-9]+(?:-[a-z0-9]+)*\.)+[a-z]{2,})(:[0-9]+)?(\/[^\?\s]+)?(\?\S+)?/ig, function() {
    var link = arguments[1] + '<a href="' + arguments[2] + '"';
    if(target) {
      link += ' target="' + target + '"';
    }
    link += '>' + arguments[2] + '</a>';
    return link;
  });

  return str;
};

Related Tutorials