js字节数
方法1:
function getBytesCount(str) { var bytesCount = 0; if (str != null) { for (var i = 0; i < str.length; i++) { var c = str.charAt(i); if (/^[u0000-u00ff]$/.test(c)) { bytesCount += 1; } else { bytesCount += 2; } } } return bytesCount; }
方法2:
function getBytesCount2(str) { if (str == null) { return 0; } else { return (str.length + str.replace(/[u0000-u00ff]/g, "").length); } }