हमें एक जावास्क्रिप्ट फ़ंक्शन लिखना है जो दो स्ट्रिंग्स में लेता है और str1 str2 में प्रकट होने की संख्या की संख्या देता है।
उदाहरण
इसके लिए कोड होगा -
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