JavaScript Full-Text search Regex - Stack Overflow

I have the following code:var key = "Force";var phrase = "May the Force be with you"

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
Add a ment  | 

2 Answers 2

Reset to default 3

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.

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

相关推荐

  • JavaScript Full-Text search Regex - Stack Overflow

    I have the following code:var key = "Force";var phrase = "May the Force be with you"

    14小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信