Match only unicode letters

13,248

Solution 1

Starting with ECMAScript 2018, JavaScript finally supports Unicode property escapes natively.

For older versions, you either need to define all the relevant Unicode ranges yourself. Or you can use Steven Levithan's XRegExp package with Unicode add-ons and utilize its Unicode property shortcuts:

var regex = new XRegExp("^\\p{L}*$")
var a = "abcäöüéèê"
if (regex.test(a)) {
    // Match
} else {
    // No Match
}

Solution 2

If you are willing to use Babel to build your javascript then there's a babel-plugin I have released which will transform regular expressions like /^\p{L}+$/ or /\p{^White_Space}/ into a regular expression that browsers will understand.

This is the project page: https://github.com/danielberndt/babel-plugin-utf-8-regex

Solution 3

You may use \p{L} with the modern ECMAScript 2018+ compliant JavaScript environments, but you need to remember that the Unicode property classes are only supported when you pass u modifier/flag:

a.match(/\p{L}+/gu)
a.match(/\p{Alphabetic}+/gu)

will match all occurrences of 1 or more Unicode letters in the a string.

NOTE that \p{Alphabetic} (\p{Alpha}) includes all letters matched by \p{L}, plus letter numbers matched by \p{Nl} (e.g. – a character for the roman number 12), plus some other symbols matched with \p{Other_Alphabetic} (\p{OAlpha}).

There are some things to bear in mind though when using u modifier with a regex:

  • You can use Unicode code point escape sequences such as \u{1F42A} for specifying characters via code points. Normal Unicode escapes such as \u03B1 only have a range of four hexadecimal digits (which equals the basic multilingual plane) (source)
  • "Characters of 4 bytes are handled correctly: as a single character, not two 2-byte characters" (source)
  • Escaping requirements to patterns compiled with u flag are more strict: you can't escape any special characters, you can only escape those that can actually behave as special characters. See HTML input pattern not working.
Share:
13,248

Related videos on Youtube

user1767962
Author by

user1767962

Updated on September 16, 2022

Comments

  • user1767962
    user1767962 over 1 year

    i have the following regex that allows only alphabets :

         /[a-zA-Z]+/
    
         a = "abcDF"
         if (a.match(/[a-zA-Z]+/) == a){
            //Match
         }else{
            //No Match
         } 
    

    How can I do this using p{L} (universal - any language like german, english etc.. )

    What I tried :

      a.match(/[p{l}]+/)
      a.match(/[\p{l}]+/)
      a.match(/p{l}/)
      a.match(/\p{l}/)
    

    but all returned null for the letter a = "aB"

  • user1767962
    user1767962 over 11 years
    I dont have problem using package, but just tell, is it mandatory to use package for checking different languageslike german, english etc..
  • user1767962
    user1767962 over 11 years
    someone told me that \w matches for any language, is it true ?
  • Tim Pietzcker
    Tim Pietzcker over 11 years
    \w only matches ASCII letters/digits/underscore in JavaScript. There is no easy way around XRegExp if you want to support Unicode.
  • Tim Pietzcker
    Tim Pietzcker over 11 years
    @user1767962: And that's going to be hard, because you'll find German words that use accented letters, English words that use "umlauts" (trema) etc., so there isn't a clear boundary between languages and their "allowed" character sets.
  • user1767962
    user1767962 over 11 years
    Invalid escape character error for ^\\p{L}*$ bcoz of two backslashes . Is it a typo ?
  • Tim Pietzcker
    Tim Pietzcker over 11 years
    @user1767962: No, in a string you need to backslashes for each literal backslash. But you do need to use the Unicode add-on, I forgot that: xregexp.com/plugins
  • izogfif
    izogfif about 5 years
    Doesn't work with \p{IsCyrillic} regex. Java does.
  • Tim Pietzcker
    Tim Pietzcker about 5 years
    @izogfif: Yes, as specified in the first document I linked to. This was a conscious decision in order to avoid ambiguity. Some languages simply ignore any is prefix, making isGreek an alternative to Greek, but that may lead to problems (Isolation?), so it was decided not to support this.
  • izogfif
    izogfif about 5 years
    @TimPietzcker is there going to be some conversion library / method / parameter so I can simply copy-paste regexps from the existing, working and tested Java code and use them in JavaScript? It's tedious to keep two copies of the same regexps in two places.
  • Tim Pietzcker
    Tim Pietzcker about 5 years
    Not that I know of. RegexBuddy can convert regexes between flavors, and although it doesn't yet support the new JS regex features, it can for example convert from Java to PCRE and give the desired result.
  • John
    John about 3 years
    This works in Chrome 89 though if ('ıi和平'.match(/\p{Alphabetic}+/gu)) {console.log('true!');} else {console.log('false!');} doesn't seem to work in Waterfox 56, thoughts please?
  • Wiktor Stribiżew
    Wiktor Stribiżew about 3 years
    @John If ECMAScript 2018 is not yet supported there, you will need a workaround, as described here.