Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 13
    Sorry how can you convert "best" into a variable in your first example? string.match(/best/i); Commented Oct 23, 2015 at 16:00
  • 7
    Why would you use .match for boolean comparison. It searches beyond the first result. You need to stop after first match which .test or .search do. Check performance here. Commented Mar 28, 2016 at 22:15
  • 1
    toLowerCase will most likely fail the Turkey Test (moserware.com/2008/02/does-your-code-pass-turkey-test.html) and similar case conversion issues. I'm not sure how ReGex handles it, but if I had to guess I'd say better. Commented Jul 11, 2016 at 11:37
  • 6
    @DougMolineux you can use the RegExp object constructor. var text = "best"; var exp = new RegExp(test, "i");. This is same as /best/i. Commented Jan 14, 2019 at 9:20
  • or if you want to avoid having to escape a lot of metacharacters It's actually not complicated. Just use RegExp.escape() which does the trick for you. Commented Dec 16, 2024 at 6:56