为什么这0会按预期返回
'https://www.site.com/abc?sk=1'.search('https://www.site.com/abc?')
但这会-1按0预期返回吗?
'https://www.site.com/abc?sk=1'.search('https://www.site.com/abc?sk')
感谢您提供任何提示。
为什么这0会按预期返回
'https://www.site.com/abc?sk=1'.search('https://www.site.com/abc?')
但这会-1按0预期返回吗?
'https://www.site.com/abc?sk=1'.search('https://www.site.com/abc?sk')
感谢您提供任何提示。
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')
<script>
if('https://www.site.com/abc?sk=1'.indexOf('https://www.site.com/abc?sk')>=0){
//Do something you want
}
</script>