Skip to content

Instantly share code, notes, and snippets.

@eneko89
Created March 8, 2016 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eneko89/f9a066cb9762f820055b to your computer and use it in GitHub Desktop.
Save eneko89/f9a066cb9762f820055b to your computer and use it in GitHub Desktop.
Looks for URLs in the source text and replaces them with anchor tags. If there are no links in the text, it does nothing.
/**
* Looks for URLs in the source text and replaces them with anchor
* tags. If there are no links in the text, it does nothing.
*
* @param {String} text Source text.
*
* @return {String} New string with URLs in the source
* text replaced with anchor tags.
*/
function anchorify(text) {
var urlRegExp = /(https?:\/\/[^\s]+)/g;
return text.replace(urlRegExp, function(url) {
return anchor(url, url);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment