Javascript String autolink()

Description

Javascript String autolink()


String.prototype.autolink = function () {
  return this.toString().replace(/((https?:\/\/|www\.)[^'"\s]*)/gi,'<a href="$1">$1</a>');
};

Javascript String autoLink()

String.prototype.autoLink = function() {
 var pattern = /(^|\s)((?:https?|ftp):\/\/([\-A-Z0-9+\u0026@#\/%?=()~_|!:,.;]*[\-A-Z0-9+\u0026@#\/%=~()_|]))/gi;
 var currentHost = document.location.host;

 return this.replace(pattern, function(match, space, url, urlWithoutProto) {
  return space + '<a' +
   ' href="' + url + '"' +
   (urlWithoutProto.split('/', 2)[0] !== currentHost ? ' target="_blank"' : '') +
  '>' + urlWithoutProto + '</a>';
 });/*  w ww.j  a  va2 s. c om*/
};



PreviousNext

Related