简体   繁体   English

如何禁止正则表达式中的数字?

[英]How do I disallow numbers in a regular expression?

Hello I was writing a Regular Expression (first time in my life I might add) but I just can't figure out how to do what I want. 您好,我在写一个正则表达式(这是我生命中的第一次),但我只是不知道该怎么做。 So far, so good since I already allow only Letters and spaces (as long as it's not the first character) now what I'm missing is that I don't want to allow any numbers in between the characters...could anybody help me please? 到目前为止,如此好,因为我已经只允许使用字母和空格(只要它不是第一个字符),现在我所缺少的是我不想在字符之间使用任何数字...有人可以帮忙吗请问我

/^[^\s][\sa-zA-Z]+[^\d\W]/

OK, what you need is: 好的,您需要的是:

/^[a-zA-Z][\sa-zA-Z]*$/

This matches: 这符合:

^           - start of line
[a-zA-Z]    - any letter
[\sa-zA-Z]* - zero or more letters or spaces
$           - the end of the line

If you want to ensure that it also ends in a letter then put another 如果要确保它也以字母结尾,请再输入一个

[a-zA-Z]

before the $ . $之前。 Note however that the string will then have to contain at least two letters (one at each end) to match. 但是请注意,字符串必须至少包含两个字母(每端一个)以匹配。

If you only want to allow letters and spaces, then what you have is almost correct: 如果只想允许字母和空格,那么您拥有的几乎是正确的:

/^[a-zA-Z][\sa-zA-Z]*$/

The $ at the end signifies the end of the string. 末尾的$表示字符串的末尾。

Edited to correct answer, thanks to @Alnitak 编辑正确的答案,感谢@Alnitak

如果要确保仅在单词之间出现空格,请使用以下命令:

/^[A-Za-z]+(?:\s+[A-Za-z]+)*$/

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

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