showing results for - "javascript linkify string text"
Boyd
05 Apr 2020
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}
Augustin
04 Jan 2017
1if(!String.linkify) {
2    String.prototype.linkify = function() {
3
4        // http://, https://, ftp://
5        var urlPattern = /\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]/gim;
6
7        // www. sans http:// or https://
8        var pseudoUrlPattern = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
9
10        // Email addresses
11        var emailAddressPattern = /[\w.]+@[a-zA-Z_-]+?(?:\.[a-zA-Z]{2,6})+/gim;
12
13        return this
14            .replace(urlPattern, '<a href="$&">$&</a>')
15            .replace(pseudoUrlPattern, '$1<a href="http://$2">$2</a>')
16            .replace(emailAddressPattern, '<a href="mailto:$&">$&</a>');
17    };
18}
Christabel
02 Oct 2020
1function Linkify(inputText) {
2    //URLs starting with http://, https://, or ftp://
3    var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
4    var replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');
5
6    //URLs starting with www. (without // before it, or it'd re-link the ones done above)
7    var replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
8    var replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
9
10    //Change email addresses to mailto:: links
11    var replacePattern3 = /(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
12    var replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
13
14    return replacedText
15}
Edoardo
13 Apr 2017
1if(!String.linkify) {
2    String.prototype.linkify = function() {
3
4        // http://, https://, ftp://
5        var urlPattern = /\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]/gim;
6
7        // www. sans http:// or https://
8        var pseudoUrlPattern = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
9
10        // Email addresses *** here I've changed the expression ***
11        var emailAddressPattern = /(([a-zA-Z0-9_\-\.]+)@[a-zA-Z_]+?(?:\.[a-zA-Z]{2,6}))+/gim;
12
13        return this
14            .replace(urlPattern, '<a target="_blank" href="$&">$&</a>')
15            .replace(pseudoUrlPattern, '$1<a target="_blank" href="http://$2">$2</a>')
16            .replace(emailAddressPattern, '<a target="_blank" href="mailto:$1">$1</a>');
17    };
18}