简体   繁体   English

正则表达式,除字母外(javascript)

[英]Regular expression anything but letters (javascript)

I want to validate a form field by checking of the input contains any letters. 我想通过检查输入中是否包含字母来验证表单字段。 All other characters and numbers should be allowed. 所有其他字符和数字都应允许。 I'm quite bad at regular expressions, and I can't find a correct solution anywhere. 我对正则表达式非常不好,在任​​何地方都找不到正确的解决方案。

I've tried this: 我已经试过了:

/[^A-Za-z]/g

but this only returns false if the string consists of only letters (ie 432ad32d should return false as well). 但这仅在字符串仅包含字母的情况下才返回false(即432ad32d也应返回false)。

Could anyone tell me how to do this? 谁能告诉我该怎么做?

Using a whitelist of allowed characters is the best approach in your case: 在您的情况下,使用允许字符白名单是最好的方法:

/^[-+\d(), ]+$/

Unicode has many things it calls a letter , better not mess with that in the first place. Unicode有很多东西叫做字母 ,最好不要把它弄乱。 And JavaScript regexes aren't well suited for handling these (they lack things like \\p{L} for instance unless you use an external library). JavaScript正则表达式不太适合处理这些(例如,除非您使用外部库,否则它们缺少\\p{L}类的东西)。

Also, by using the whitelist approach you can be sure about the kinds of inputs which will be accepted by your form. 另外,通过使用白名单方法,您可以确定表单将接受的输入种类。 You can't predict the kind of mess users could input otherwise. 您无法预测用户可能输入的混乱情况。 Think about things like this : 想想事情是这样

TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉͠P̯͍̭Ó̚N̐Y̡H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ

:-) :-)

/[^A-Za-z]/

This regex matches a single non-letter, which isn't very useful. 此正则表达式与单个非字母匹配,这不是很有用。 Yura Yakym's answer matches the beginning of the string, any number of non-letters, and then the end of the string, which is useful when it matches: it means your string contains only those things. Yura Yakym的答案匹配字符串的开头,任意数量的非字母,然后匹配字符串的结尾,这在匹配时很有用:这意味着您的字符串仅包含那些内容。

Another useful regex is: 另一个有用的正则表达式是:

/[A-Za-z]/

This matches a single letter, which is useful when it doesn't match : it means your string does not contain any letters at all. 这匹配一个字母, 当它不匹配时非常有用:这意味着您的字符串根本不包含任何字母。

For your question in general, "how can I ensure a string lacks letters?", I would use that second regex: I would try to match a letter, and hopefully fail to do so. 对于您的一般问题,“如何确保字符串中没有字母?”,我将使用第二个正则表达式:我将尝试匹配字母,但希望不这样做。 For input validation though, I'd prefer a regex that describes all possible valid inputs. 不过,对于输入验证,我希望使用正则表达式来描述所有可能的有效输入。 If /^[^A-Za-z]*$/ does so, then use that. 如果/^[^A-Za-z]*$/这样做,则使用它。 If you have additional requirements, add those to it. 如果您还有其他要求,请将其添加到其中。 Don't have multiple "no letters? OK. no non-dash special characters? OK." 没有多个“没有字母?好。没有非破折号的特殊字符?好。” ... well, unless you want to provide error messages precisely about such things. ...好吧,除非您想提供有关此类事情的错误消息。

试试这个正则表达式: ^[^A-Za-z]*$

You need to include anchors 您需要包括锚点

/^[^A-Za-z]+$/g

This will ensure the string starts and ends with one or more numbers/special characters 这样可以确保字符串以一个或多个数字/特殊字符开头和结尾

You forgot about start and end markers. 您忘记了开始和结束标记。 Also you don't need g flag. 另外,您不需要g标志。

/^[^A-Za-z]*$/

Anyway, that's strange as I can enter ciryllic letters still. 无论如何,这很奇怪,因为我仍然可以输入圆形字母。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM