I am trying to validate below format in regex but I am not successful.
I have tried below regex
"^[0-9]{1,6}X{1}$"
"^[0-9]{1,6}X$"
It doesn't satisfy all conditions.
Required Format:
So I need a regex format for
You missed making the X optional.
Do this:
^[0-9]{1,6}X*$ /gm
This will mean text starting with one to six digits, ending in zero or more X.
Demo
For case-insensitivity, use the /i flag:
^[0-9]{1,6}X*$ /gmi
Demo
Alternatively, you can also use a character set with both small and capital letters:
^[0-9]{1,6}[Xx]*$ /gm
Demo
A positive lookahead will restrict the maximum length as desired:
^(?=.{1,6}$)[0-9]*[Xx]*$ /gm
Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With