हमें एक JavaScript फंक्शन लिखना है जो दो स्ट्रिंग्स लेता है और पहली स्ट्रिंग के दूसरी स्ट्रिंग में प्रकट होने की संख्या लौटाता है।
मान लें हमारी स्ट्रिंग है -
const main = 'This is the is main is string';
हमें उपरोक्त "main" स्ट्रिंग में नीचे दी गई स्ट्रिंग की उपस्थिति ढूंढनी है -
const sub = 'is';
उदाहरण
const main = 'This is the is main is string';
const sub = 'is';
const countAppearances = (main, sub) => {
const regex = new RegExp(sub, "g");
let count = 0;
main.replace(regex, (a, b) => {
count++;
});
return count;
};
console.log(countAppearances(main, sub));
आउटपुट
कंसोल में निम्नलिखित आउटपुट है -
4