[英]Regular Expression in Javascript to start with Uppercase, only alphabets, maxlength 20, minlength 2
I can validate this rule using plain Javascript.我可以使用纯 Javascript 验证此规则。 But I would prefer to do it using RegEx.但我更愿意使用 RegEx 来做到这一点。 I tried this (and other variations) but am not too confident:我试过这个(和其他变体)但不太自信:
regEx = /^[A-Z]([a-z]|[A-Z]){1,19}$/;
Test cases did go through.测试用例确实通过了。 Is this the right way or is there a better approach?这是正确的方法还是有更好的方法?
You may need a tool for build your regEx like https://regex101.com您可能需要一个工具来构建您的 regEx,例如https://regex101.com
What do you want to match exactly ?你想完全匹配什么?
Your regex match the following string: - Start by a character between [AZ] => only alphabetical upper case - 1 to 19 alphabetical character either upper or lower case您的正则表达式匹配以下字符串: - 以 [AZ] 之间的字符开始 => 仅大写字母 - 1 到 19 个大写或小写字母
So your string length is between 2 and 20 characters.所以你的字符串长度在 2 到 20 个字符之间。
You could simply your regExp by你可以简单地通过你的regExp
regEx = /^[A-Z][a-zA-Z]{1,19}$/;
If you want no min length, mean that empty string match you could use:如果您不想要最小长度,则表示您可以使用空字符串匹配:
regEx = /^([A-Z][a-zA-Z]{0,19})?$/
If you want min lenght to be 1, means that you match single upper character you could use:如果您希望最小长度为 1,则意味着您匹配可以使用的单个大写字符:
regEx = /^[A-Z][a-zA-Z]{0,19}$/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.