I need to write a regex for validating a string. The regular expression should pass the string if it contains any of the following: y
, Y
, yes
, YES
, 1
. The letters can be in any case. I am new to regular expression and JavaScript.
I need to write a regex for validating a string. The regular expression should pass the string if it contains any of the following: y
, Y
, yes
, YES
, 1
. The letters can be in any case. I am new to regular expression and JavaScript.
- 2 Have you tried anything? Looks simple – Tushar Commented Aug 17, 2015 at 5:25
- 'hello y here is yes i am Y you are Yes'.match(/yes|y|1/gi) remove g if you know that only one of them exists – Harpreet Singh Commented Aug 17, 2015 at 5:27
- contains any of the following: do you mean "contains", or do you mean "exactly equal to"? – user663031 Commented Aug 24, 2015 at 2:38
1 Answer
Reset to default 9You need to add an optional group as well as a case-insensitive i
modifier.
/y(?:es)?|1/i.test(str)
or
/[1y](?:es)?/i.test(str)
or
/[y1]/i.test(str)
For doing exact match.
/^(?:y(?:es)?|1)$/i.test(str)