// 字符串中的英文、数字、特殊字符等为一个字节,中文汉字是两个字节function countByte(str) {let length = 0;for (let i = 0; i < str.length; i++) {// 先看看它是不是双字节if (/[\u4e00-\u9fa5]/g.test(str[i])) {length += 2;} else {length += 1;}}return length;}console.log(countByte('你好hello world'));
