Make links and emails clickable from paragraphs in UI

When user enters text in paragraph for mailto, http or incorrect http link and we want to make it clickable use below javascript code.
EX :-

FROM –

Hello these are my links www.google.com , http://www.google.com or contact us to test@inheritx.com

TO

Hello these are my links www.google.com , http://www.google.com or contact us to test@inheritx.com

function linkify(inputText) {
var replacedText, replacePattern1, replacePattern2, replacePattern3;

//URLs starting with http://, https://, or ftp://
replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
replacedText = inputText.replace(replacePattern1, ‘$1‘);

//URLs starting with “”. (without // before it, or it’d re-link the ones done above).
replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
replacedText = replacedText.replace(replacePattern2, ‘$1$2‘);

//Change email addresses to mailto:: links.
replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim;
replacedText = replacedText.replace(replacePattern3, ‘$1‘);

return replacedText;
}

You may also like

Leave a Reply