I don’t know about other Javascript engines, but in V8, including Chrome and Node.js, String.prototype.search behaves in an unexpected fashion.
> "054".search("0.4")
0 // expected -1
> "Sample String 007".search("0.7")
14 // expected -1
> "Sample String 0.7".search("0.7")
14 // expected behavior
If this is the expected behavior, why is that the case? And if this is the expected behavior, how do I properly search for a String without regular expressions coming in to play?
MDN’s page on String.search has this to say about the function’s argument:
Therefore, the strings in your examples are correctly coerced into a regular expression objects. Your tests are equivalent to:
and they return the correct result.
As @meetamit notes for your second question, you actually want
indexOf, which expects a string argument, not a regular expression.