Search code examples
javascriptutf-8xregexp

javascript regex support for UTF8 characters with Number and letters


How to generate regex for supporting utf8 characters with Numbers [0-9], letters [a-z] and [A-Z].

I am using xregexp library to support utf-8 characters.

Till now i used :

  1. XRegExp("^\p{L}\d{0-9}+$") //Not supporting any of them.
  2. XRegExp("[^\p{N}\p{L}]") //Not supporting any of them.
  3. XRegExp("^\p{L}+$") // Supports only UTF-8 characters, [a-z] and [A-Z] and not [0-9]

Thanks.


Solution

  • Just use \\p{L}+ in combination with [0-9]:

    XRegExp("^(\\p{L}|[0-9])+$")
    

    Note that you need to escape your backslash, there: \\.

    To include _/-:

    XRegExp("^(\\p{L}|[0-9_/-])+$")
    

    Note that the dash (-) has to be the first or last character in those brackets, due to it's special meaning.