2

为什么这0会按预期返回

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

但这会-10预期返回吗?

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

感谢您提供任何提示。

4

2 回答 2

3

because the ? has a special meaning in regular expressions.

Use indexOf instead (which works with plain strings) when you don't need regular expressions:

'https://www.site.com/abc?sk=1'.indexOf('https://www.site.com/abc?sk')
于 2013-10-03T12:08:23.997 回答
1
<script>    
if('https://www.site.com/abc?sk=1'.indexOf('https://www.site.com/abc?sk')>=0){ 
   //Do something you want
}
</script>
于 2013-10-03T12:22:21.620 回答