Home >Web Front-end >JS Tutorial >Calculate Sina Weibo message length (you can also enter 119 characters)_javascript skills

Calculate Sina Weibo message length (you can also enter 119 characters)_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:30:241027browse

This article is a method for calculating the message length of Sina Weibo. That is (please abide by the community convention when speaking, and you can enter 119 characters).

Copy code The code is as follows:

var getMessageLength = (function() {
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(message) {
message = message || '';
message = message.replace(/rn/g, "n");
var c = 41 ,
d = 140,
e = 20,
f = message,
g = message.match(/http://[a-zA-Z0-9] (.[a- zA-Z0-9] ) ([-A-Z0-9a-z_$. !*()/,:;@&=?~#%]*)*/gi) || [],
h = 0;

for(var i = 0, j = g.length; i < j; i ) {
var k = byteLength(g[i]);
if(/ ^(http://t.cn)/.test(g[i])) {
continue;
}
/^(http://) (t.sina.com.cn| t.sina.cn)/.test(g[i]) || /^(http://) (weibo.com|weibo.cn)/.test(g[i]) ? h = k <= c ? k : k <= d ? e : k - d e : h = k <= d ? e : k - d e;
f = f.replace(g[i], "");
}
var l = Math.ceil((h byteLength(f)) / 2);
return l;
};
})();

getMessageLength('The hard drive of my computer broke yesterday. I sent an email to the National Security Agency, but they refused to send me the backup content! '); //39
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn