How can I use Unicode-aware regular expressions in JavaScript?

Situation for ES 6

The ECMAScript language specification, edition 6 (also commonly known as ES2015), includes Unicode-aware regular expressions. Support must be enabled with the u modifier on the regex. See Unicode-aware regular expressions in ES6 for a break-down of the feature and some caveats.

ES6 is widely adopted in both browsers and stand-alone Javascript runtimes such as Node.js, so using this feature won’t require extra effort in most cases. Full compatibility list:

Situation for ES 5 and below (legacy browsers)

There is a transpiler named regexpu that translates ES6 Unicode regular expressions into equivalent ES5. It can be used as part of your build process. Try it out online..

Even though JavaScript operates on Unicode strings, it does not implement Unicode-aware character classes and has no concept of POSIX character classes or Unicode blocks/sub-ranges.

  • Issues with Unicode in JavaScript regular expressions

  • Check your expectations here: Javascript RegExp Unicode Character Class tester (Edit: the original page is down, the Internet Archive still has a copy.)

  • Flagrant Badassery has an article on JavaScript, Regex, and Unicode that sheds some light on the matter.

  • Also read Regex and Unicode here on SO. Probably you have to build your own “punctuation character class”.

  • Check out the Regular Expression: Match Unicode Block Range builder (archived copy), which lets you build a JavaScript regular expression that matches characters that fall in any number of specified Unicode blocks.

    I just did it for the “General Punctuation” and “Supplemental Punctuation” sub-ranges, and the result is as simple and straight-forward as I would have expected it:

  • There also is XRegExp, a project that brings Unicode support to JavaScript by offering an alternative regex engine with extended capabilities.

  • And of course, required reading: – JavaScript has a Unicode problem:

Leave a Comment