简体   繁体   English

带字符串的正则表达式包含Java中通配符/ Min,MaxLength的数字

[英]Regular Expression With String Contain Digit With WildCard/Min,MaxLength In Javascript

I am new to JavaScript Regular Expressions. 我是JavaScript正则表达式的新手。 Currently I am trying to create a regex on a String that may contain a digit. 目前,我正在尝试在可能包含数字的字符串上创建一个正则表达式。

Scenario is like following:< 场景如下:<

The String may contain a * either before or after or both but not in the middle. 字符串可以在*之前或之后或两者之间都包含* ,但不能在中间包含*

The String length will be a minimum of 2 characters and a maximum of 12 characters (not counting the * ). 字符串长度至少为2个字符,最大为12个字符(不包括* )。

Input String Example: 输入字符串示例:

AB1542378522
AC6546457869
OA6546457869

Other valid Strings are: 其他有效的字符串是:

*154*
*C6*
AB*
AB154237*
*2378522
*C654645
*645
*AB*
*AC6546457869*
OA*

Invalid Strings: 无效的字符串:

*15*4
*15*4*
*A*B15*42*37*

Only the * wildcard is supported, no other special character allowed in input. 仅支持*通配符,输入中不允许其他特殊字符。 For example in above input string AB , AC and OA are valid first two character but not AA , AX , OS ... then it will follow 10 digits at max. 例如,在上面的输入字符串ABACOA中,前两个字符是有效的,但AAAXOS ...则不是有效的,那么它将最多跟随10个数字。

I am unable to create such regex that covers above use-cases. 我无法创建涵盖上述用例的正则表达式。 My code is getting exhausted by if else branching. 如果没有其他分支,我的代码将变得筋疲力尽。 If I find any answer I will post it in a comment. 如果我找到任何答案,我会在评论中发布。 Make a comment for any confusion. 如有任何疑问,请发表评论。

Try to use this pattern: 尝试使用此模式:

/^\*?(?=[^*]{2})[A-Z]{0,2}[0-9]{0,10}\*?$/

pattern explanation: 模式说明:

^           begining of the string
\*?         optional * 
(?=[^*]{2}) check if at least 2 characters that are not * follows
[A-Z]{0,2}  between zero and 2 letters
[0-9]{0,10} between zero and 10 digits
\*?         optional *
$           end of the string

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

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