开发者

javascript regex to replace url of certain length with

开发者 https://www.devze.com 2023-01-12 15:32 出处:网络
I want a regex which will replace the long url with ..., for example, I pass in a string with url in it, i.e

I want a regex which will replace the long url with ..., for example,

I pass in a string with url in it, i.e

<a href="http://www.abc.com/thisSite/myPages/blah-blah-blah.aspx">http://www.abc.com/thisSite/myPages/blah-blah-blah.aspx</a>

should be converted to

<a href="http://www.abc.com/thisSite/myPages/blah-blah-blah.a开发者_Go百科spx">http://www.abc.com/th....</a>

any help would be appriciated.


Jquery

$('a').each(function(){
   var text = $(this).text();
   if (text.length > 20 && text.substr(0,7) == "http://"){ // it looks like a URL and it's long
      text = text.substr(0,20) + '&hellip;';
      $(this).text(text);
   }

});

normal javascript

var links = document.getElementsByTagName('a');
for (var i = 0;i=links.length-1;i++) {
   var text = links[i].innerHTML;
   if (text.length > 20 && text.substr(0,7) == "http://"){ // it looks like a URL and it's long
      text = text.substr(0,20) + '&hellip;';
      links[i].innerHTML = text;
   }
}

I have no idea if this works or not as I wrote it from my head. have fun.


Based on this solution, here is what you could do :

String.prototype.trunc =
     function(n,useWordBoundary,wordChar){
         if (!wordChar) wordChar = ' ';
         var toLong = this.length>n,
             s_ = toLong ? this.substr(0,n-1) : this;
         s_ = useWordBoundary && toLong ? s_.substr(0,s_.lastIndexOf(wordChar)) : s_;
         return toLong ? s_ + '&hellip;' : s_;
     };

Then simply apply this to anchors displaying links :

var maxLength = 40;
var aElements = document.getElementsByTagName('a'), _text, _param;
var aLen = aElements.length;

for (var i=0; i<aLen; i++) {
   _text = aElements[i].innerHTML;
   // remove url params
   if (0 <= (_param = _text.indexOf('?'))) {
      _text = _text.substr(0, _param - 1);
   }
   if (0 == _text.indexOf('http://') && _text.length > maxLength) {
      _text = _text.trunc(maxLength);
      // _text = _text.trunc(maxLength, true, '/'); 
   }
   aElements[i].innerHTML = _text;
}


You'll need to use the following bit of code

<script>
var cutOffAt = 20;
var anchorRef = getAnchorFromDom();
var text = anchorRef.innerHtml;
if(text.length > 20) {
  anchorRef.innerHtml = text.substr(0, cutOffAt);
}

function getAnchorFromDom()
{
  // provide your own logic here
  return document.getElementsByTagName('a')[0];
}
</script>

Untested.


And Here i was thinking you actually wanted to use a regexp and never mentioned jquery .. headslap ...

var s = '<a href="http://www.abc.com/thisSite/myPages/blah-blah-blah.aspx">http://www.abc.com/thisSite/myPages/blah-blah-blah.aspx</a>';
var t = s.replace(/(<a [^>]*>)([^<]{1,20})([^<]*)(<\/a>)/, function(str, p1, p2, p3, p4) { return p1 + p2 + (p3.length ? '...' : '') + p4; });
document.write(t);

http://jsfiddle.net/5kSzL/

0

精彩评论

暂无评论...
验证码 换一张
取 消