7

Recently I discovered, to my surprise, that JavaScript has no built-in support for Unicode regular expressions.

So how can I test a string for letters only, Unicode or ASCII?

LarsH
  • 27,481
  • 8
  • 94
  • 152
Thomas
  • 4,641
  • 13
  • 44
  • 67

2 Answers2

10

I'd recommend Steven Levithan's excellent XRegExp library, which has a Unicode plugin containing various Unicode character classes: http://xregexp.com/plugins/

Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • Hi there. I downloaded the library but I cannot get it to work. I am testing with the examples provided by the author but I don't get a match. Running unicodeWord.test("Русский"); (found on the author's site) returns false – Thomas Dec 10 '10 at 12:01
  • @Thomas: it works perfectly for me: http://jsfiddle.net/timdown/GAKYt/. Which browser are you using? – Tim Down Dec 10 '10 at 14:03
3

Recently I discovered, to my surprise, that javascript has no builtin support for unicode regex.

This comes to a surprise to me as well because

alert(/\u00B6/.test("¶"));

prints true.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • OK. My bad. So what is the correct regular expression to check that a string contains only letters, in every character set? Can this be done? – Thomas Dec 10 '10 at 08:42
  • 5
    The `\uHHHH` notation is a JavaScript *language* feature. Unicode *regex* support means things like Unicode properties (`\p{L}`), blocks (`\p{InLatinExtendedA}`), and scripts (`\p{Cyrillic}`). JavaScript regexes have nothing like that; you either have to use the XRegExp Unicode plugin, or switch to a different language. – Alan Moore Dec 10 '10 at 22:31
  • For those who may not catch Darin's meaning: He's saying (with a little sarcasm) that Javascript actually *does* (did) have builtin support for unicode regex. – LarsH Oct 19 '18 at 07:45