汉子1个计为1个,数字2个计为1个
var str = '我的北京大学12311';
function getByteLen (str) {
let len = 0, strLen = str.length;
for (let i = 0; i < strLen; i++) {
if (str[i].match(/[^\x00-\xff]/ig) != null) {
len += 1
} else { len += 0.5 }
}
return Math.ceil(len)
};
console.log(getByteLen(str));