String length calculation, interception, URL parameter acquisition

Source: Internet
Author: User

1. Chinese-English mixed character interception

//Chinese length interception calculation, it is advisable to mix in English, the number up rounding, accuracy of 1 English characters error, an English is a character, a Chinese character counted as a char.     //Sub ("Chinese Zlsd", 1), "Medium"    //Sub ("Chinese Zlsd", 3), "Chinese"     functionSubch (str,n) {varr=/[^\x00-\xff]/G; if(Str.replace (R, "MM"). Length<=n) {returnstr;} varM=math.floor (N/2); for(vari=m;i<str.length;i++){             if(Str.substr (0,i). Replace (R, "MM"). length>=N) {                 returnSTR.SUBSTR (0, i); }         }         returnstr; } 

2. Remove spaces before and after characters

Remove space before and after string
function () { returnthis. replace (/^\s*/, ""). Replace (/\s*$/, "");

3. Get string lengths in English and Chinese

// get a Chinese-English string length, one for one, one for English    function getstrlength (str) {        var cArr = Str.match (/[^\x00-\xff]/ig);         return null ? 0 : carr.length);    }

4. Get url parameter length

// url parameter get    function (parm) {          varnew RegExp ("(^|&)" + parm + "= ([^&]*] (&|$)");           var this. substr (this. indexOf ("\?") +1). Match (reg);           if (r!=nullreturnreturnnull;      

String length calculation, interception, URL parameter acquisition

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.