简体   繁体   English

使用sublime文本在jquery插件验证引擎的JS中使用条件的正则表达式

[英]Regex with condition in JS for jquery plugin validation engine using sublime text

I'm trying to achieve a regex pattern in java script for the following statement/description: 我正在尝试在java脚本中为以下语句/描述实现正则表达式模式:

A string beginning with one letter character, only x or y, if x the next element should be the number 0 otherwise if y the next element should be 1 and followed for a sequence with 4 digits. 以一个字母字符开头的字符串,仅为x或y,如果x,则下一个元素应为数字0,否则,如果y,则下一个元素应为1,并且后跟4个数字的序列。

For example, X01234, Y10000. 例如,X01234,Y10000。

I have built something like this 我已经建立了这样的东西

\ˆ(X|Y)(X?0|Y?1)\d{4}\

But of course I`m getting wrong match pattern result.My regex is ignoring and match whatever the character is X followed by 0 or 1 and the same for Y. 但当然我得到错误的匹配模式结果。我的正则表达式忽略并匹配任何字符是X后跟0或1和Y相同。

Bellow is described the logic structure for it: 贝娄描述了它的逻辑结构:

正则表达式可视化

Debuggex Demo Debuggex演示

Could someone help me to fix it and show me what I'm doing wrong? 有人可以帮我修理它并告诉我我做错了什么吗?

PS: For clarification purpose, sometimes the JSLInt of the sublime text is showing warning that this regex has a syntax error, and the Jquery validation plugin doesn't works as expected for custom validation using it. PS:为了澄清目的,有时崇高文本的JSLInt显示警告此正则表达式具有语法错误,并且Jquery验证插件无法按预期使用它进行自定义验证。

Thanks 谢谢

You can try this: 你可以试试这个:

^(?:X0|Y1)[0-9]{4}$

Note: in Javascript, the pattern delimiter is the slash / 注意:在Javascript中,模式分隔符是斜杠/

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

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