Equivalent Javascript substr_count?

I am trying to find an equivalent function for Javascript. What I'm trying to do is look for the current url, and if it has the next first part of the url, do something.

In PHP, I have this

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

So, if it matches this URL and all additional URLs like / shop / shop -gallery / product1..etc, the statement will be true.

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

Thanks guys!

+3
source share
3 answers

, , ? , Frosty Z , , substr_count PHP, strpos (, , ).

JS - String indexOf:

if (stringToSearch.indexOf(current_url) > -1)  {
   doSomething();
}

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/indexOf

+5

Substr_count JavaScript :

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

Source: https://habr.com/ru/post/1771756/


All Articles