为什么这会返回一个0
预期为什么这个string.search不能按预期工作?
'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
预期为什么这个string.search不能按预期工作?
'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')
感谢任何提示。
因为?
在正则表达式中有特殊含义。
使用indexOf
,而不是(与普通字符串作品),当你不需要正则表达式:
'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>
?你为什么要做'> ='?那么它也会匹配'http://fishing.com/https:// www.site.com' – Thilo
''是在正则表达式保留字符,您可能需要逃避它 –