简体   繁体   English

javascript中的正则表达式问号

[英]regex question mark in javascript

It is probably quite simple, but I do not how to do. 这可能很简单,但我不怎么做。

I have this regex : 我有这个正则表达式:

new RegExp("^[A-Za-z\\u00C0-\\u017F][\\- ]?+$");

It validates a first name. 它验证名字。 The name have to begin with a letter (the range is in unicode and works fine) and then continue with letters or - or space. 名称必须以字母开头(范围是unicode并且工作正常)然后继续使用字母或 - 或空格。 But it can be just letters, as in most names. 但它可以只是字母,就像大多数名字一样。

I have searched but I didn't find the right way to do it. 我搜索过但我没有找到正确的方法。

I don't want to duplicate the character range. 我不想复制角色范围。 It is just to have a code more "proper". 只是让代码更“合适”。

If you could help, it would be great :) 如果你能提供帮助,那就太棒了:)

Thanks in advance 提前致谢

Right now you only allow one letter and then one or more dashes/spaces. 现在你只允许一个字母,然后是一个或多个破折号/空格。 You probably want 你可能想要

new RegExp("^[A-Za-z\\u00C0-\\u017F][A-Za-z\\u00C0-\\u017F -]*$");

But in general, trying to validate a name with regexes isn't such a good idea. 但总的来说,尝试用正则表达式验证名称并不是一个好主意。

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

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