我不了解其他 Javascript 引擎,但在 V8 中,包括 Chrome 和Node.js,String.prototype.search
其行为方式出乎意料。
> "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
如果这是预期的行为,为什么会这样?如果这是预期的行为,我如何在没有正则表达式的情况下正确搜索字符串?