Javascript 为什么string.search不';你不能按预期工作吗?

Javascript 为什么string.search不';你不能按预期工作吗?,javascript,string,Javascript,String,为什么会按预期返回0 'https://www.site.com/abc?sk=1“.搜索('https://www.site.com/abc?) 但是这会返回一个-1,因为预期0 'https://www.site.com/abc?sk=1“.搜索('https://www.site.com/abc?sk) 感谢您提供的各种提示。因为?在正则表达式中有特殊的含义 当您不需要正则表达式时,请改用indexOf(可用于普通字符串): 'https://www.site.com/abc?sk=1'.

为什么会按预期返回
0

'https://www.site.com/abc?sk=1“.搜索('https://www.site.com/abc?)

但是这会返回一个
-1
,因为预期
0

'https://www.site.com/abc?sk=1“.搜索('https://www.site.com/abc?sk)


感谢您提供的各种提示。

因为
在正则表达式中有特殊的含义

当您不需要正则表达式时,请改用
indexOf
(可用于普通字符串):

'https://www.site.com/abc?sk=1'.indexOf('https://www.site.com/abc?sk')

因为
在正则表达式中有特殊的含义

当您不需要正则表达式时,请改用
indexOf
(可用于普通字符串):

'https://www.site.com/abc?sk=1'.indexOf('https://www.site.com/abc?sk')

如果('https://www.site.com/abc?sk=1'.indexOf('https://www.site.com/abc?sk')>=0){ 
//做你想做的事
}

如果('https://www.site.com/abc?sk=1'.indexOf('https://www.site.com/abc?sk')>=0){ 
//做你想做的事
}

是正则表达式中的保留字符,您可能需要将其转义
是正则表达式中的保留字符,您可能需要将其转义为什么要执行
=
?然后它也会匹配
http://fishing.com/https://www.site.com
你为什么要这样做?然后它也会匹配
http://fishing.com/https://www.site.com