javascript中的search()方法无法在包含“\\ch”的字符串中搜索它

javascript中的search()方法无法在包含“\\ch”的字符串中搜索它,javascript,string,search,slash,Javascript,String,Search,Slash,我试图找到latex代码的\\ch的位置,比如foo\\ch,但string的搜索方法无法找到它。例如,运行以下代码: console.log('foo\\c'.search('\\c')); // expected 3, really get 3 console.log('foo\\ch'.search('\\ch')); // expected 3, but get -1 我怀疑该错误是由特殊字符引起的,但我已在internet上搜索过,它似乎不是特殊字符。当使用字符串作为参数调用搜索时,

我试图找到latex代码的\\ch的位置,比如foo\\ch,但string的搜索方法无法找到它。例如,运行以下代码:

console.log('foo\\c'.search('\\c')); // expected 3, really get 3
console.log('foo\\ch'.search('\\ch')); // expected 3, but get -1
我怀疑该错误是由特殊字符引起的,但我已在internet上搜索过,它似乎不是特殊字符。

当使用字符串作为参数调用搜索时,它将转换为正则表达式请参见

但是新的RegExp'\\ch'返回/\ch/,它是匹配任何退格字符的正则表达式\cX匹配控制字符X,请参阅

要实现您想要的功能,请使用/\\ch/regex

'Foo\\ch'.search(/\\ch/) // returns 3