Regular expression to match ASCII and Unicode letters
Asked Answered
F

2

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?

Floats answered 10/12, 2010 at 7:44 Comment(0)
B
10

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

Botha answered 10/12, 2010 at 10:12 Comment(2)
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 falseFloats
@Thomas: it works perfectly for me: jsfiddle.net/timdown/GAKYt. Which browser are you using?Botha
M
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.

Meissen answered 10/12, 2010 at 7:55 Comment(3)
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?Floats
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.Finstad
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.Semantics

© 2022 - 2024 — McMap. All rights reserved.