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,652
  • 12
  • 14

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
  • 1,025
  • 5
  • 20
Narkhede Tushar
  • 665
  • 3
  • 16
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
  • 1,025
  • 5
  • 20
Hema Ramasamy
  • 686
  • 1
  • 6
  • 16