Posted at

javascript でバイト長を取得

More than 5 years have passed since last update.

var bytes = function(s) {

return encodeURIComponent(s).split(/(?:%..|.)/).length - 1;
};

bytes('a%d'); // => 3

// UTF8 な文字
bytes('ω'); // => 2
bytes(''); // => 3
bytes('