turn text into links javascript

Solutions on MaxInterview for turn text into links javascript by the best coders in the world

showing results for - "turn text into links javascript"
Vincent
29 May 2019
1function linkify(inputText) {
2    var replacedText, replacePattern1, replacePattern2, replacePattern3;
3
4    //URLs starting with http://, https://, or ftp://
5    replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
6    replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');
7
8    //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
9    replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
10    replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
11
12    //Change email addresses to mailto:: links.
13    replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim;
14    replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
15
16    return replacedText;
17}