Question

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:

Regular expression visualization

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

Was it helpful?

Solution

You can try this:

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top