简体   繁体   English

Javascript中的正则表达式以大写开头,只有字母,maxlength 20,minlength 2

[英]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.

相关问题 正则表达式仅检查字母表 - Regular expression to check only alphabets 使用正则表达式验证javascript中通用字母的输入 - Validate input with regular expression for universal alphabets in javascript 正则表达式,用于匹配仅出现在字符串开头或末尾的字母之间的'(单引号)的单次出现 - Regular expression for matching single occurrence of ' (single quote) coming only between alphabets not on start or end of the string 带空格的字母的正则表达式 - Regular Expression for alphabets with spaces Javascript-正则表达式两个大写,小写和数字 - Javascript - Regular Expression For two uppercase, lowercase and numbers 带字符串的正则表达式包含Java中通配符/ Min,MaxLength的数字 - Regular Expression With String Contain Digit With WildCard/Min,MaxLength In Javascript 正则表达式只接受字符串第 1 位的字母 - Regular Expression to accept only alphabets in the 1st place of the string javascript正则表达式允许名称带有一个空格和特殊字母 - javascript regular expression allow name with one space and special Alphabets JavaScript正则表达式(当且仅当) - JavaScript Regular Expression (If and only if) 长度为 6 到 20 个字符的正则表达式,支持大小写字母和数字字符 - regular expression for length 6 to 20 characters with upper and lower case alphabets and numeric character support
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM