Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>JavaScript strings are UCS-2 encoded but can represent Unicode code points outside the Basic Multilingual Pane (<code>U+0000</code> - <code>U+D7FF</code> and <code>U+E000</code> - <code>U+FFFF</code>) using two 16 bit numbers (a UTF-16 <em>surrogate pair</em>), the first of which must be in the range <code>U+D800</code> - <code>U+DFFF</code>.</p> <p>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):</p> <pre><code>function containsSurrogatePair(str) { return /[\uD800-\uDFFF]/.test(str); } alert( containsSurrogatePair("foo") ); // false alert( containsSurrogatePair("f") ); // true </code></pre> <p>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:</p> <pre><code>var getStringCodePoints = (function() { function surrogatePairToCodePoint(charCode1, charCode2) { return ((charCode1 &amp; 0x3FF) &lt;&lt; 10) + (charCode2 &amp; 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 &lt; str.length) { charCode = str.charCodeAt(i); if ((charCode &amp; 0xF800) == 0xD800) { codePoints.push(surrogatePairToCodePoint(charCode, str.charCodeAt(++i))); } else { codePoints.push(charCode); } ++i; } return codePoints; } })(); alert( getStringCodePoints("f").join(",") ); // 102,119558 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload