เราต้องเขียนฟังก์ชันที่รับสองสตริงและคืนค่าจำนวนครั้งที่ 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