测试字符串是否只包含字母(a-z +éüöêåø等...) [英] Test if string contains only letters (a-z + é ü ö ê å ø etc..)

查看:307
本文介绍了测试字符串是否只包含字母(a-z +éüöêåø等...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想匹配一个字符串,以确保它只包含字母。

I want to match a string to make sure it contains only letters.

我有这个并且它运作正常:

I've got this and it works just fine:

var onlyLetters = /^[a-zA-Z]*$/.test(myString);

但是

因为我也说另一种语言,我需要允许所有字母,而不仅仅是AZ。例如:

Since I speak another language too, I need to allow all letters, not just A-Z. Also for example:

é ü ö ê å ø

有没有人知道全局'alpha'这个术语是否包含与regExp一起使用的所有字母?或者甚至更好,有没有人有某种解决方案?

does anyone know if there is a global 'alpha' term that includes all letters to use with regExp? Or even better, does anyone have some kind of solution?

非常感谢

编辑:
刚刚意识到你可能也想允许' - '和''加上双重名字,如:'Mary-Ann'或'Mary Ann'

Just realized that you might also wanna allow '-' and ' ' incase of a double name like: 'Mary-Ann' or 'Mary Ann'

推荐答案

我不知道这样做的实际原因,但是如果你想用它作为用户昵称的登录名的预检,我会建议你自己输入字符,不要使用你在unicode中找到的整个'alpha'字符,因为你可能不会在以下字母中找到光学差异:

I don’t know the actual reason for doing this, but if you want to use it as a pre-check for, say, login names oder user nicknames, I’d suggest you enter the characters yourself and don’t use the whole ‘alpha’ characters you’ll find in unicode, because you probably won’t find an optical difference in the following letters:

А ≠ A ≠ Α  # cyrillic, latin, greek

在这种情况下,如果您想最大限度地减少帐户伪造等,最好手动指定允许的字母。

In such cases it’s better to specify the allowed letters manually if you want to minimise account faking and such.

添加

好吧,如果它是一个应该是非独特的领域,我是也应该允许希腊语。当我强迫用户将其名称更改为拉丁版本时,我感觉不舒服。

Well, if it’s for a field which is supposed to be non-unique, I would allow greek as well. I wouldn’t feel well when I force users into changing their name to a latinised version.

但是对于昵称这样的独特字段,您需要为其他访问者提供该网站一个提示,它确实是他们认为的昵称。很糟糕,人们会伪造帐户,我已经和我交换了。当然,这取决于您的用户;但我相信我认为最好只允许基本的拉丁语+变音符号。 (也许看一下这个列表: Latin-derived_alphabet

But for unique fields like nicknames you need to give your other visitors of the site a hint, that it’s really the nickname they think it is. Bad enough that people will fake accounts with interchanging I and l already. Of course, it’s something that depends on your users; but to be sure I think it’s better to allow basic latin + diacritics only. (Maybe have a look at this list: Latin-derived_alphabet)

作为未经测试的建议(带' - ','_'和''):

As an untested suggestion (with ‘-’, ‘_’ and ‘ ’):

/^[a-zA-Z\-_ ’'‘ÆÐƎƏƐƔIJŊŒẞÞǷȜæðǝəɛɣijŋœĸſßþƿȝĄƁÇĐƊĘĦĮƘŁØƠŞȘŢȚŦŲƯY̨Ƴąɓçđɗęħįƙłøơşșţțŧųưy̨ƴÁÀÂÄǍĂĀÃÅǺĄÆǼǢƁĆĊĈČÇĎḌĐƊÐÉÈĖÊËĚĔĒĘẸƎƏƐĠĜǦĞĢƔáàâäǎăāãåǻąæǽǣɓćċĉčçďḍđɗðéèėêëěĕēęẹǝəɛġĝǧğģɣĤḤĦIÍÌİÎÏǏĬĪĨĮỊIJĴĶƘĹĻŁĽĿʼNŃN̈ŇÑŅŊÓÒÔÖǑŎŌÕŐỌØǾƠŒĥḥħıíìiîïǐĭīĩįịijĵķƙĸĺļłľŀʼnńn̈ňñņŋóòôöǒŏōõőọøǿơœŔŘŖŚŜŠŞȘṢẞŤŢṬŦÞÚÙÛÜǓŬŪŨŰŮŲỤƯẂẀŴẄǷÝỲŶŸȲỸƳŹŻŽẒŕřŗſśŝšşșṣßťţṭŧþúùûüǔŭūũűůųụưẃẁŵẅƿýỳŷÿȳỹƴźżžẓ]$/.test(myString)

另一个编辑:
我添加了撇号对于像奥尼尔或奥莱利这样的人来说。 (对于那些无法正确输入卷曲的人来说,直的和反向的撇号。)

Another edit: I have added the apostrophe for people with names like O’Neill or O’Reilly. (And the straight and the reversed apostrophe for people who can’t enter the curly one correctly.)

这篇关于测试字符串是否只包含字母(a-z +éüöêåø等...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆