Convert the url in CONENT to be clickable and make it shorter.
For example:
Convert the content
Balabala The link is http://www.google.com/?aaaaabefaaaaaaaaaaaafeeafaeff3asefffffffffff
to
Bala开发者_如何学Gobala The link is <a href=http://www.google.com/?aaaaabefaaaaaaaaaaaafeeafaeff3asefffffffffff>http://www.google.com/?aa...</a>
Something like this may help:
<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>
Here is a function that replaces any URLs inside a string with an anchor:
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>"
The "cut" length can be passed as the second argument.
Something like:
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>";
精彩评论