Nicklas
Nicklas

Reputation: 5

Regex matching with yes/no

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

Answers (1)

TeWu
TeWu

Reputation: 6556

There are the regular expressions:

But 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

Related Questions