4 * Calculate the byte length of a string (accounting for UTF-8).
6 * @author Jan Paul Posma, 2011
8 jQuery.byteLength = function ( str ) {
10 // This basically figures out how many bytes a UTF-16 string (which is what js sees)
11 // will take in UTF-8 by replacing a 2 byte character with 2 *'s, etc, and counting that.
12 // Note, surrogate (\uD800-\uDFFF) characters are counted as 2 bytes, since there's two of them
13 // and the actual character takes 4 bytes in UTF-8 (2*2=4). Might not work perfectly in
14 // edge cases such as illegal sequences, but that should never happen.
16 .replace( /[\u0080-\u07FF\uD800-\uDFFF]/g, '**' )
17 .replace( /[\u0800-\uD7FF\uE000-\uFFFF]/g, '***' )