JS 验证留出空间
我在 JS 中有 regExp 来验证名字 var regExp = /^[A-Za-z]$/;
,并且我想在其中留出空格,我该怎么办?
现在它只允许 a 到 z 和 A 到 Z 字符,我只需要留出空间。
提前致谢。
I have regExp in JS to validate first/last names var regExp = /^[A-Za-z]$/;
, and I want to allow space with it, how can i?
Right now its allowing a to z and A to Z chars only, i only need to allow space.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@ThiefMaster 的解决方案展示了如何向正则表达式添加空格。
但是,如果您的目的是匹配名称,那么您可能可以做得更好。例如,您可以说有效名称是任何一个或多个有效的 unicode 字母字符(改编自 XRegExp) :
[编辑]
请注意,上述表达式匹配任何具有一个或多个 Unicode 字母的名称,包括包含其他字符的名称(例如“San Juan”、“O'Connors”或“Anne-Marie”) ”)。
The solution by @ThiefMaster shows how you can add a space to your regular expression.
However, if your intent is to match a name then you can probably do better. For example, you could say that a valid name is any one or more valid unicode letter characters (adapted from XRegExp):
[Edit]
Note that the above expression matches any name which has one or more Unicode letter including names which include other characters (like "San Juan", "O'Connors", or "Anne-Marie").
您无法通过任何正则表达式验证自然名。所以请停止这样做——这只会让那些名字中的字符与你的正则表达式不匹配的用户生气。
无论如何,
/^[A-Za-z ]$/
是您正在寻找的正则表达式。You cannot validate natural names by any regular expression. So please stop doing it - it just pisses of users who have characters in their name not matchign your regex.
Anyway,
/^[A-Za-z ]$/
is the regex you are looking for.