将文本链接转换为超链接和锚文本 [英] Converting Text Link to Hyper Link and Anchor Text
本文介绍了将文本链接转换为超链接和锚文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对将文本链接转换为超级链接有疑问.我通过使用此链接中的代码成功完成了此操作;如何用链接替换纯URL?
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>
现在我要获取的不是链接形式的锚文本,应该没有'http://www.'
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.
推荐答案
这有效!
function replaceURLWithHTMLLinks(text) {
var exp = /(\b(http|ftp|https):\/\/([\w-]+\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?)/ig;
return text.replace(exp,"<a href='$1'>$3</a>");
}
输入文字
"http://stackoverflow.com/questions/579335/javascript-regexp-to-wrap-urls-and-emails-in-anchors";
输出:
<a href='http://stackoverflow.com/questions/579335/javascript-regexp-to-wrap-urls-and-emails-in-anchors'>stackoverflow.com</a>
这是一个正常工作的 JsFiddle
这篇关于将文本链接转换为超链接和锚文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文