Equivalent Javascript substr_count?

user381800 picture user381800 · Oct 27, 2010 · Viewed 10.2k times · Source

I am trying to find an equivalent function for Javascript. What I am trying to do is look for the current URL and if it has the following first part of the URL then do something.

In PHP I have this

if (substr_count($current_url, $root . $_SERVER['SERVER_NAME'] . '/shop/shop-gallery') {
 doSomething();
}

So as long as it matches that URL and all sub URLs like /shop/shop-gallery/product1..etc, the statement will be true.

Now how can I execute the same exact statement in javascript?

Thanks guys!

Answer

GeekTantra picture GeekTantra · Mar 3, 2011

Substr_count in JavaScript can also be implemented as follows:

var substr_count = hay_stack_string.split('search_substring').length - 1;