3

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 replaceURLWithHTMLLinks(text) {
    var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
    return text.replace(exp,"<a href='$1'>$1</a>"); 
}

The output of this code is like;

Input: http://www.tinymoviez.com Output: <a href='http://www.tinymoviez.com'>http://www.tinymoviez.com</a>

Now what i want is to get anchor text not in link form, it should be without 'http://www.'

In short i need the output as

<a href='http://www.tinymoviez.com'>tinymoviez.com</a>

But keep in mind that it should be auto, i will convert bulk of links at a time.

How can i do that??? Any help will be appreciated.

Community
  • 1
  • 1
wicky439
  • 31
  • 3

1 Answers1

0

This works!!!

function replaceURLWithHTMLLinks(text) {
    var exp = /(\b(http|ftp|https):\/\/([\w-]+\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?)/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

Alberto Fontana
  • 928
  • 1
  • 14
  • 35
  • Yes it works somehow, but if i have www.mywebsite.com/blabla link. Then it will Get only "www.mywebsite" Only.... :( – wicky439 Dec 10 '13 at 17:22
  • Can i have only website Name.... Like "Stackoverflow" only, either it have www or not.... Any help will be highly appreciated – wicky439 Dec 10 '13 at 17:23
  • It only converts short links. With long links it give error at output. Like mywebsite.com/jhdkhdhkjhdkjhdkhdkhdkhkhdkdhkhdkhdkhkdhkjdhkjhdkhdkhdkhkdhkdhd – wicky439 Dec 11 '13 at 13:26