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?
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?
Share Improve this question edited Jun 26, 2011 at 5:51 painotpi 6,9922 gold badges39 silver badges72 bronze badges asked Jun 26, 2011 at 5:43 User1234894User1234894 132 silver badges4 bronze badges 1- Hi fellas. How do I use search's case-insensitive flag with a variable (not plain-text)? – User1234894 Commented Jun 26, 2011 at 5:57
2 Answers
Reset to default 3The 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.
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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745429954a4627347.html
评论列表(0条)