In js, the string is intercepted based on the number of words, and the url_javascript cannot be truncated.

Source: Internet
Author: User
For a text, the output text is intercepted and 400 characters are retained. It is troublesome to retain the url, in particular, when there are two identical URLs, you cannot use indexOf to obtain their character locations. Today, you will receive a request:
1. Extract the output text and retain 400 characters.
2. If the content to be intercepted is a url, the complete url address is retained.
3. Add a ellipsis ......
----
It is difficult to retain the url, especially when there are two identical URLs, indexOf cannot be used to obtain its character location.
Processing result:

Related code:

The Code is as follows:


String. prototype. sizeAt = function (){
Var nLen = 0;
For (var I = 0, end = this. length; I NLen + = this. charCodeAt (I) & gt; 128? 2:1;
}
Return nLen;
};
String. prototype. cutStr = function (n, sCut ){
If (this. sizeAt () <= n ){
Return this;
}
SCut = sCut | "";
Var max = n-sCut.sizeAt ();
Var nLen = 0;
Var s = this;
For (var I = 0, end = this. length; I NLen + = this. charCodeAt (I) & gt; 128? 2:1;
If (nLen> max ){
S = this. slice (0, I );
S + = sCut;
Break;
}
}
Return s. toString ();
};
String. prototype. cutStrButUrl = function (n, sCut ){
If (this. sizeAt () <= n ){
Return this. toString ();
}
SCut = sCut | "";
Var max = n-sCut.sizeAt ();
Var s = this;
// Search for all included URLs
Var aUrl = s. match (/https? : \ // [A-zA-Z0-9] + (\. [a-zA-Z0-9] +) + ([-_ A-Z0-9a-z \ $ \. \ + \! \ * \/, :;@&= \? \~ \ # \ %] *) */Gi );
// When the maximum character is between URLs, bCut is set to flase;
Var bCut = true;
If (aUrl ){
// Judge each url
For (var I = 0, endI = aUrl. length; I Var sUrl = aUrl [I];
// There may be two identical URLs
Var aP = s. split (sUrl );
Var nCurr = 0;
Var nLenURL = sUrl. sizeAt ();
Var sResult = "";
For (j = 0, endJ = aP. length; j NCurr + = aP [j]. sizeAt ();
SResult + = aP [j];
SResult + = sUrl;
// The current number of words is less than max, but the number of words added to the url exceeds max.
If (nCurr <max & nCurr + nLenURL> max ){
S = sResult + sCut;
BCut = false;
Break;
}
NCurr + = nLenURL;
}
If (bCut = false ){
Break;
}
};
}
If (bCut ){
S = s. cutStr (n, sCut );
}
Return s. toString ();
};
Console. log ('20 characters intercepted normally '. cutStrButUrl (20 ,'......'));
Console. log ('20 characters are intercepted normally, but I have exceeded '. cutStrButUrl (20 ,'......'));
Console. log ('Can you truncate the url string http://www.baidu.com? '. CutStrButUrl (20 ,'......'));
Console. log ('HTTP: // www.baidu.com has two strings with the same url http://www.baidu.com? '. CutStrButUrl (51 ,'......'));

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.