हमें एक जावास्क्रिप्ट फंक्शन लिखना है जो दो स्ट्रिंग्स ले और 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