将CONTENT中的网址转换为可点击并缩短。
例如:
转换内容
Balabala The link is http://www.google.com/?aaaaabefaaaaaaaaaaaafeeafaeff3asefffffffffff
到
Balabala The link is <a href=http://www.google.com/?aaaaabefaaaaaaaaaaaafeeafaeff3asefffffffffff>http://www.google.com/?aa...</a>
答案 0 :(得分:0)
这样的事情可能有所帮助:
<div id='result'>
</div>
<script>
var content = "The link is http://www.google.com/?q=hello visit it!";
var result = content.replace(/(http:\/\/(www\.)?([^(\s)]+))/, "<a href='$1'>$3</a>");
document.getElementById('result').innerHTML = result;
</script>
答案 1 :(得分:0)
这是一个用锚点替换字符串中的任何URL的函数:
function linkify(str){
var url = 'http://www.google.com/?aaaaabefaaaaaaaaaaaafeeafaeff3asefffffffffff';
return str.replace(/https?\:\/\/\S+/, function(url){
var shortUrl = url.substring(0, 20) + '...';
return shortUrl.link(url)
})
}
linkify("Balabala The link is http://www.google.com/?aaaaabefaaaaaaaaaaaafeeafaeff3asefffffffffff")
// "Balabala The link is <a href="http://www.google.com/?aaaaabefaaaaaaaaaaaafeeafaeff3asefffffffffff">http://www.google.com/?aaaaabe...</a>"
“cut”长度可以作为第二个参数传递。
答案 2 :(得分:-1)
类似的东西:
len = 15; //set how long of a string you would like to show
url = "http://www.google.com/?aaaaabefaaaaaaaaaaaafeeafaeff3asefffffffffff";
//if the original url is smaller than or equal to the length than do nothing
//otherwise take the len number of chars and append ... after
shorturl = (url.length <= len) ? url : url.substr(0,len) + "...";
link = "<a href='" + url + "'>" + shorturl + "</a>";