0

How to find the String Count in a String in JavaScript?

Ex:

https://localhost/public/10e900fe-0470-11eb-93c6-f377ab03a336//

Get the // occurrence count.

Thushara Buddhika
  • 1,336
  • 10
  • 12

2 Answers2

2

var string = "https://localhost/public/10e900fe-0470-11eb-93c6-f377ab03a336//";
console.log((string.match(/\/\//g) || []).length);

You can try below code:

var string = "https://localhost/public/10e900fe-0470-11eb-93c6-f377ab03a336//";
console.log((string.match(/\/\//g) || []).length);
Makwana Prahlad
  • 859
  • 3
  • 18
Narkhede Tushar
  • 647
  • 3
  • 14
2

var a="https://localhost/public/10e900fe-0470-11eb-93c6-f377ab03a336//"
var count= a.split("//").length-1;
console.log(count)

you can find the count by splitting it into an array and find the length of the array

var a="https://localhost/public/10e900fe-0470-11eb-93c6-f377ab03a336//"
var count= a.split("//").length-1; // its 2
Makwana Prahlad
  • 859
  • 3
  • 18
Hema Ramasamy
  • 646
  • 1
  • 5
  • 14