Skip to content

Instantly share code, notes, and snippets.

@Last-Order
Created May 9, 2019 11:45
Show Gist options
  • Save Last-Order/e7792d6d6c9e63ce08db652fec597763 to your computer and use it in GitHub Desktop.
Save Last-Order/e7792d6d6c9e63ce08db652fec597763 to your computer and use it in GitHub Desktop.
微博字数计算
getWeiboLength = (text) => {
const getLength = a => {
if (!a) return 0;
// eslint-disable-next-line no-control-regex
const b = a.match(/[^\x00-\xff]/g);
return a.length + (b ? b.length : 0);
};
const c = 41,
d = 140,
e = 20,
g = text.match(/(http|https):\/\/[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+([-A-Z0-9a-z_\$\.\+\!\*\(\)\/\,\:;@&=\?~#%]*)*/gi) || [];
let h = 0;
let f = text;
for (let i = 0, j = g.length; i < j; i++) {
const k = getLength(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], '');
}
const l = Math.ceil((h + getLength(f)) / 2);
return l;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment