onlineracoon
onlineracoon

Reputation: 2970

Javascript Node.js Unicode Regular Expression

I need input validation, good input validation to keep everything clean. So I want a strict regular expression on "full name", international so I need Unicode support. I downloaded XRegExp, which compiles "their format" to regular expressions and supports Unicode however:

Correct names:

Incorrect names:

So I need:

  1. Match unicode letter (a, æ)
  2. Match unicode accents (á, Ë etc.)
  3. Match unicode hyphens (- _ etc)

In PHP I can do the following operations to match:

\p{L} (unicode letter)
\p{Mn} (unicode accents)
\p{Pd} (unicode hyphens)

What I figured out, that I can do with XRegExp:

^\p{L}+$ would match '日本' so only the letter one works, but how can I figure out how to do these unicode accents, hypens?

Thanks for help.

Upvotes: 2

Views: 1755

Answers (1)

slevithan
slevithan

Reputation: 1414

It looks like @lanzz gave you some good feedback about potential issues with name validation, but for the record, you can use the Unicode categories you mentioned with XRegExp just as you would with PHP. E.g., XRegExp('^[\\p{L}\\p{Mn}\\p{Pd} ]+$'). This requires XRegExp's Unicode Categories addon. Note that the backslashes are escaped due to JavaScript string literal escaping rules.

Upvotes: 2

Related Questions