AGamePlayer
AGamePlayer

Reputation: 7736

Why this string.search doesn't work as expected?

Why this returns a 0 as expected

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

But this returns a -1 as 0 is expected?

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

Thanks for any kind of tips.

Upvotes: 2

Views: 563

Answers (2)

Joke_Sense10
Joke_Sense10

Reputation: 5402

<script>    
if('https://www.site.com/abc?sk=1'.indexOf('https://www.site.com/abc?sk')>=0){ 
   //Do something you want
}
</script>

Upvotes: 1

Thilo
Thilo

Reputation: 262504

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')

Upvotes: 4

Related Questions