Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match non-ASCII (German, Spanish, etc.) letters in regex?

I was unable to find or create a regex which match only letters,spaces, accented letters and spanish and german letters.

I'm using this for now:

var reg = new RegExp("^[a-z _]*$");

I've tried:

^[:alpha: _]*$   
^[a-zA-Z0-9äöüÄÖÜ]*$  
^[-\p{L}]*$   

Any idea? Or the regex supported by javascript engines are limited?

like image 240
Damiano Barbati Avatar asked Nov 25 '25 21:11

Damiano Barbati


2 Answers

The 2nd to last case looks like it should work, but is missing a " " and "_":

/^[a-zA-Z0-9äöüÄÖÜ]*$/.test("aäöüÄÖÜz") => true in FF 3.6 and IE8

/^[a-zA-Z0-9äöüÄÖÜ]*$/.test("é") => false in FF 3.6 and IE8

I'm am unable to find the other constructs in the ECMAScript specification.

Happy coding.

Edit Also check the page encoding and make sure it is "unicode" (UTF-8 likely). If this can't be ensured, then use the \uXXXX escape sequences in the regular expression (using the escapes can be done anyway and may help with source code editing/control).

I'm parsing a name input field, and this seems to be working for both German and French:

^[a-zA-Z\-ÀàÂâÆæÇçÈèÉéÊêËëÎîÏïÔôŒœÙùÛûÜü]*$

Some folks have names like 'Rölf-Dieter', and this lets them through, while checking for numbers. A little extreme, but it works!

like image 32
ghettosoak Avatar answered Nov 28 '25 09:11

ghettosoak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!