-3

Something similar to this, How to check if the URL contains a given string?

but is there a way to check if the URL contains a slash (/) more than 3 times, for example?

Thanks!

Community
  • 1
  • 1
claras
  • 173
  • 2
  • 3
  • 11

1 Answers1

3

This is one possibility, all characters that are not "/" are removed, then the String.length is used to find how many characters remain.

Javascript

var url = "https://stackoverflow.com/questions/16449482/check-to-see-a-url-contains-a-certain-character-how-many-times";

console.log(url.replace(/[^\/]/g, "").length);

On jsfiddle

This is not the fastest method though, I just wanted to give an alternative to those that have already been suggested in a previous QA. I added it to the jsperf

Community
  • 1
  • 1
Xotic750
  • 22,914
  • 8
  • 57
  • 79