Reputation: 13
I have the following code:
var key = "Force";
var phrase = "May the Force be with you";
I need to search for the key in phrase in a case-insensitive way.
It needs to be fast and reliable. I think a custom regex may be best here, but am not sure what is the best way to implement the same.
Any suggestions? What's the best way to do this in JavaScript?
Upvotes: 1
Views: 5181
Reputation: 360
str = "May the Force be with you";
str.search(/Force/i);
This is a regular expression with the i flag, which makes it case insensitive.
Upvotes: 2
Reputation:
The i
flag is used for case-insensitive search:
phrase.search(/force/i);
You can learn more at the Mozilla docs for the search function and regular expressions.
You can also test your regular expressions (minus JavaScript specific features) at regexpal.
Upvotes: 3