Regex / u modifier in JavaScript?

I recently created a regular expression for my PHP code that allows only letters (including special characters plus spaces), but now I have a problem converting it (?) To a JavaScript compatible regular expression .: /^[\s\p{L}]+$/u , the problem is the /u modifier at the end of the regular expression pattern, since JavaScript does not allow such a flag.

How can I rewrite this so that it will work in JavaScript too?

Is there anything to allow only Polish characters: Ł , Ą, Ś , Ć , ...

+14
javascript php regex unicode
source share
3 answers

The /u modifier is designed to support Unicode . Support has been added to JavaScript in ES2015.

Read https://stackoverflow.com/a/166268/ for more information about Unicode in regular expressions with JavaScript.


Polish characters:

 Ą \u0104 Ć \u0106 Ę \u0118 Ł \u0141 Ń \u0143 Ó \u00D3 Ś \u015A Ź \u0179 Ż \u017B ą \u0105 ć \u0107 ę \u0119 ł \u0142 ń \u0144 ó \u00F3 ś \u015B ź \u017A ż \u017C 

All special Polish characters:

 [\u0104\u0106\u0118\u0141\u0143\u00D3\u015A\u0179\u017B\u0105\u0107\u0119\u0142\u0144\u00F3\u015B\u017A\u017C] 
+16
source share

There is no UTF-8 string concept in JavaScript, so you are unlikely to need the /u flag. (Your lines are probably already in the usual JavaScript form, one UTF-16 code for each character.)

The big problem is that JavaScript does not support \p{L} or any equivalent notation; JavaScript regular expressions have no idea about Unicode character properties. See Responses to https://stackoverflow.com/a/166188/ for some ways to approximate it.


Edited to add: If you need to support only Polish letters, you can write /^[\sa-zA-ZĄĆĘŁŃÓŚŹŻąćęłńóśźż]+$/ . Parts of az and az cover the letters ASCII, and the remaining letters are listed separately.

+3
source share

All Articles