简体   繁体   中英

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:

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.

For example, 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.

Bellow is described the logic structure for it:

正则表达式可视化

Debuggex Demo

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.

Thanks

You can try this:

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

Note: in Javascript, the pattern delimiter is the slash /

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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