Reputation: 5
So I'm trying to match yes/no with regex. Using "yes" as an example here: What I want the user to be able to put in is basically either y or Y followed by whatever characters.
What I've tried so far is [Yy]\w+|[Yy]
and that works but, viewed with my not so experienced regex eyes, that looks a bit redundant?
Upvotes: 0
Views: 2282
Reputation: 6556
There are the regular expressions:
^[Yy].*
regex demo^[Nn].*
regex demoBut I would suggest that it's better to just take the first character of a string, convert it to lower case, and check if it's either y
or n
. For example in JavaScript:
const reply = "Yes";
const isItYes = reply[0].toLowerCase() === "y"; // <== true
Upvotes: 1