Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to match ASCII and Unicode letters

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?

like image 296
Thomas Avatar asked Dec 10 '10 07:12

Thomas


People also ask

What does \u mean in regex?

U (Unicode dependent), and re. X (verbose), for the entire regular expression. (The flags are described in Module Contents.) This is useful if you wish to include the flags as part of the regular expression, instead of passing a flag argument to the re.

What is the regex for Unicode paragraph seperator?

\u000d — Carriage return — \r. \u2028 — Line separator. \u2029 — Paragraph separator.

What is \p l in regex?

\p{L} matches a single code point in the category "letter". \p{N} matches any kind of numeric character in any script. Source: regular-expressions.info.

What does regex (? S match?

Therefore, the regular expression \s matches a single whitespace character, while \s+ will match one or more whitespace characters.


2 Answers

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

like image 167
Tim Down Avatar answered Sep 21 '22 06:09

Tim Down


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.

like image 31
Darin Dimitrov Avatar answered Sep 19 '22 06:09

Darin Dimitrov