Mathias Bynens: JavaScript ♥ Unicode

JSConf · 6K views · 2014 · 25:42

Open on YouTube

Web developmentPython, Ruby & scripting languagesLanguage & linguisticstalk

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about code points, surrogate pairs, emoji and Unicode-aware regexes in JavaScript.

Recommended by 1 people across 1 different months, 2014 to 2020.

What it is

A 2014 JSConf EU talk by Mathias Bynens on how JavaScript relies on Unicode. It covers Unicode basics, why JS strings expose UCS-2/UTF-16 code units, the problems this causes with astral symbols, ES5 workarounds, and the ES6 improvements for strings and regular expressions.

What people said

Recommends Useless Code · 2016-06 · Stack Overflow · on “How to sort json data first with numeric,second capital letters with alphabetic sort and then third alphabetically?”

Although it requires more code, I would use the first approach, it is more future-proof. Right now you might not have to support anything more than ASCII but in the future you might. If you want to localize your code for other languages, many non-English languages (especially Asian ones) make heavy use of surrogate pairs. For more info about code-points and Unicode I would recommend this talk [this video] .

Cites Rafael Xavier · 2014-11 · Stack Overflow · on “Javascript regex Currency symbol in a string”

Short answer : /[\$\xA2-\xA5\u058F\u060B\u09F2\u09F3\u09FB\u0AF1\u0BF9\u0E3F\u17DB\u20A0-\u20BD\uA838\uFDFC\uFE69\uFF04\uFFE0\uFFE1\uFFE5\uFFE6]/ Long answer : A JavaScript regular expression equivalent to /\p{Sc}/ is: ScRe = /[\$\xA2-\xA5\u058F\u060B\u09F2\u09F3\u09FB\u0AF1\u0BF9\u0E3F\u17DB\u20A0-\u20BD\uA838\uFDFC\uFE69\uFF04\uFFE0\uFFE1\uFFE5\uFFE6]/ ScRe.test("$"); // true ScRe.test("£"); // true ScRe.test("€"); // true The above has been generated by: $ npm install regenerate $ npm install unicode-7.0.0 $ node > regenerate().add(require('unicode-7.0.0/categories/Sc/symbols')).toString(); [LINK] [LINK] Watch: [this video]

Cites a-c-sreedhar-reddy · 2020-11 · Stack Overflow · on “Generating an emoji from unicode and then extracting unicode gives a different unicode”

Code point values range from U+0000 to U+10FFFF. Initially JavaScript had Unicode escape sequences which consist of exactly 4 hexadecimal digits to represent a code point. U+0000 to U+FFFF could be represented using these. eg: >> 'I \u2661 JavaScript!' 'I ♡ JavaScript!' In ES6, JS introduced Unicode code point escapes . >> '\u{1F4A9}' '' // U+1F4A9 PILE OF POO Between the braces, you can use up to six hexadecimal digits, which is enough to represent all Unicode code points. For backward compatibility with ECMAScript 5 and older environments, the unfortunate solution is to use surrogate pairs: >> '\uD83D\uDCA9' '' // U+1F4A9 PILE OF POO ECMAScript 6 introduces String.prototype.codePointAt(pos