Skip to content Skip to sidebar Skip to footer

Converting Text Link To Hyper Link And Anchor Text

I have a question about converting text links to hyper links. I did it successfully by using this code from this link; How to replace plain URLs with links? function replaceURLWith

Solution 1:

This works!!!

function replaceURLWithHTMLLinks(text) {
    var exp = /(\b(http|ftp|https):\/\/([\w-]+\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?)/ig;
    return text.replace(exp,"<a href='$1'>$3</a>"); 
}

Input text

"http://stackoverflow.com/questions/579335/javascript-regexp-to-wrap-urls-and-emails-in-anchors";

Output:

<a href='http://stackoverflow.com/questions/579335/javascript-regexp-to-wrap-urls-and-emails-in-anchors'>stackoverflow.com</a>

Here is a working JsFiddle


Post a Comment for "Converting Text Link To Hyper Link And Anchor Text"