开发者

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?


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜