0

Use case:

I have the following string '파이어 볼' and I want to check if each symbol in the string is a valid letter in any language.

What I've tried:

To do this, I found the following regex pattern that is supposed to match "any letter in any language".

/\p{L}/

The pattern successfully matched on the major regex helper websites (regex101, regexcoach, regexer) I tested. They all returned the 4 symbols as matches, as intended.

Now when I try this in my javascript/node.js application it doesn't work, see the following snippet:

if (/\p{L}/.test('파이어 볼')) {
  alert('true'); 
}
else {
  alert('false');
}

Question:

How can I properly test if a given character is a valid letter in any language.

kentor
  • 16,553
  • 20
  • 86
  • 144
  • can you please explain which can be invalid characters? – programtreasures Dec 20 '17 at 03:49
  • @programtreasures all "non-letter" characters, such as interpunction, numbers, non printable, control-characters and so on – kentor Dec 20 '17 at 03:51
  • 2
    JS regex does not support `\p{L}` and similar; you were probably testing with a flavor like PCRE which supports it. There are alternatives that have been suggested already, for example my answer [here](https://stackoverflow.com/a/37668315/6083675). – Laurel Dec 20 '17 at 03:51

1 Answers1

1

Javascript doesn't properly unicode regex. Use this library as it fixes the missing functionality:

https://www.npmjs.com/package/js-regex-pl

Riki
  • 1,776
  • 1
  • 16
  • 31