I want to check if a variable is in any of a set of words. For example
'Guava'.match(/b(apple|mango|orange)b/ig);
The above does not match, which is correct.
'AppleA'.match(/b(apple|mango|orange)b/ig)
This too does not match. Again correct.
'Apple.A'.match(/b(apple|mango|orange)b/ig)
This however returns a match, which is not what I want. How do I fix this?
I want a match only if the value is Apple
OR apple
OR mango
OR mAngo
, etc.
2
Answers
Try this
'Apple.A'.match(/^(apple|mango|orange)$/ig)
To check if a string is one of several strings in JavaScript, case-insensitively, you can convert the string to lowercase with the
toLowerCase
method, and pass it to theincludes
method of the array of several strings to check against: