I copied it from Sina Weibo Code. It is very good. If you need it, refer to it.
The Code is as follows:
/*
* Copied from Sina Weibo
**/
Var getLength = (function (){
Var trim = function (h ){
Try {
Return h. replace (/^ \ s + | \ s + $/g ,"")
} Catch (j ){
Return h
}
}
Var byteLength = function (B ){
If (typeof B = "undefined "){
Return 0
}
Var a = B. match (/[^ \ x00-\ x80]/g );
Return (B. length + (! A? 0: a. length ))
};
Return function (q, g ){
G = g | {};
G. max = g. max | 140;
G. min = g. min | 41;
G. surl = g. surl | 20;
Var p = trim (q). length;
If (p> 0 ){
Var j = g. min,
S = g. max,
B = g. surl,
N = q;
Var r = q. match (/(http | https): \ // [a-zA-Z0-9] + (\. [a-zA-Z0-9] +) + ([-A-Z0-9a-z \ $ \. \ + \! \ _ \ * \ (\) \/\, \ :;@&= \?~ # %] *) */Gi) | [];
Var h = 0;
For (var m = 0,
P = r. length; m <p; m ++ ){
Var o = byteLength (r [m]);
If (/^ (http: \/t.cn)/. test (r [m]) {
Continue
} Else {
If (/^ (http: \/) + (weibo.com | weibo.cn)/. test (r [m]) {
H + = o <= j? O: (o <= s? B: (o-s + B ))
} Else {
H + = o <= s? B: (o-s + B)
}
}
N = n. replace (r [m], "")
}
Return Math. ceil (h + byteLength (n)/2)
} Else {
Return 0
}
}
})();