How to count how many repetitions a particular character has

Asked 2 years ago, Updated 2 years ago, 85 views

var temp = "This is a string.";
alert(temp.count("is")); // output '2'

I'm looking for a function that counts how many strings in a particular string into a parameter when I insert a string like this, but is there a function like that in JavaScript?

javascript regex string

2022-09-22 11:39

1 Answers

You can do it simply by writing a regular expression.

var temp = "This is a string.";

var count = (temp.match(/is/g) || []).length;

alert(count);


2022-09-22 11:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.