swedish characters check with javascript?

2020-07-18 05:23发布

How can I rewrite this code to check for all characters including the swedish å, ä, and ö characters?

      alphaExp = /^[a-zA-Z]+$/;

The above only checks against english letters!

The swedish contains å, ä, ö coming after the Z... rest is all the same as english.

Thanks

3条回答
三岁会撩人
2楼-- · 2020-07-18 05:37

Did you try /^[a-zA-ZäöåÄÖÅ]+$/?

In my Firefox, /^[a-zA-ZäöåÄÖÅ]+$/.test("öÅ"); evaluates to true.

查看更多
▲ chillily
3楼-- · 2020-07-18 05:41

You have PHP and JavaScript marked in here. For the JavaScript answer, see the previous answer from Matt Ball.

For the PHP answer, see \p and friends. \p{L} for example.

php -r '$foo = "täst"; if ( preg_match("/^\\p{L}+$/",$foo) ) echo "yay\n";'
查看更多
地球回转人心会变
4楼-- · 2020-07-18 05:53

Pretty much straight from the horse's mouth:

Support for internationalization in JavaScript's RegExp is virtually nonexistent.

See the posts following the linked one for how to get around this (by defining your own character class).

查看更多
登录 后发表回答