আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা দুটি স্ট্রিং নেবে এবং প্রথম স্ট্রিংটি দ্বিতীয় স্ট্রিং-এ কতবার আসেছে তার কাউন্ট রিটার্ন করবে।
ধরুন, আমাদের স্ট্রিংটি হল −
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