Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if a string contains multibyte characters in Javascript?

Tags:

Is it possible in Javascript to detect if a string contains multibyte characters? If so, is it possible to tell which ones?

The problem I'm running into is this (apologies if the Unicode char doesn't show up right for you)

s = "𝌆";  alert(s.length);    // '2' alert(s.charAt(0)); // '��' alert(s.charAt(1)); // '��' 

Edit for a bit of clarity here (I hope). As I understand it now, all strings in Javascript are represented as a series of UTF-16 code points, which means that regular characters actually take up 2 bytes (16 bits), so my usage of "multibyte" in the title was a bit off. Some characters do not fall in the Basic Multilingual Plane (BMP), such as the string in the example above, and so they take up two code points (32 bits). That is the question I was asking. I'm also not editing the original title, since to someone who doesn't know much about this stuff (and hence would be searching SO for info about it), "multibyte" would make sense.

like image 837
nickf Avatar asked Feb 02 '11 16:02

nickf


People also ask

How do you check if a string contains a specific character in JavaScript?

Use the String. includes() method to check if a string contains a character, e.g. if (str. includes(char)) {} . The include() method will return true if the string contains the provided character, otherwise false is returned.

Is a multibyte character?

A multibyte character is a character composed of sequences of one or more bytes. Each byte sequence represents a single character in the extended character set. Multibyte characters are used in character sets such as Kanji. Wide characters are multilingual character codes that are always 16 bits wide.

What is a multibyte string?

A null-terminated multibyte string (NTMBS), or "multibyte string", is a sequence of nonzero bytes followed by a byte with value zero (the terminating null character). Each character stored in the string may occupy more than one byte.

How do you write a multibyte character?

If supported by your input device, multibyte characters can be entered directly. Otherwise, you can enter any multibyte character in the ASCII form \[N], where N is the 2-, 4-, 6-, 7-, or 8-digit hexadecimal encoding for the character.


1 Answers

JavaScript strings are UCS-2 encoded but can represent Unicode code points outside the Basic Multilingual Pane (U+0000 - U+D7FF and U+E000 - U+FFFF) using two 16 bit numbers (a UTF-16 surrogate pair), the first of which must be in the range U+D800 - U+DFFF.

Based on this, it's easy to detect whether a string contains any characters that lie outside the Basic Multilingual Plane (which is what I think you're asking: you want to be able to identify whether a string contains any characters that lie outside the range of code points that JavaScript represents as a single character):

function containsSurrogatePair(str) {     return /[\uD800-\uDFFF]/.test(str); }  alert( containsSurrogatePair("foo") ); // false alert( containsSurrogatePair("f𝌆") ); // true 

Working out precisely which code points are contained in your string is a little harder and requires a UTF-16 decoder. The following will convert a string into an array of Unicode code points:

var getStringCodePoints = (function() {     function surrogatePairToCodePoint(charCode1, charCode2) {         return ((charCode1 & 0x3FF) << 10) + (charCode2 & 0x3FF) + 0x10000;     }      // Read string in character by character and create an array of code points     return function(str) {         var codePoints = [], i = 0, charCode;         while (i < str.length) {             charCode = str.charCodeAt(i);             if ((charCode & 0xF800) == 0xD800) {                 codePoints.push(surrogatePairToCodePoint(charCode, str.charCodeAt(++i)));             } else {                 codePoints.push(charCode);             }             ++i;         }         return codePoints;     } })();  alert( getStringCodePoints("f𝌆").join(",") ); // 102,119558 
like image 99
Tim Down Avatar answered Sep 20 '22 11:09

Tim Down