
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Count appearances of a string in another - JavaScript
We are required to write a JavaScript function that takes in two strings and returns the count of the number of times the first string appears in the second string
Let’s say our string is −
const main = 'This is the is main is string';
We have to find the appearance of the below string in the above “main” string −
const sub = 'is';
Let’s write the code for this function −
Example
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));
Output
Following is the output in the console −
4
- Related Articles
- Count and return the number of characters of str1 that makes appearances in str2 using JavaScript
- Function that only replaces character from string after specified appearances in JavaScript
- Number of times a string appears in another JavaScript
- Count occurrences of a string that can be constructed from another given string in C++
- Count total punctuations in a string - JavaScript
- Finding count of special characters in a string in JavaScript
- Can part of a string be rearranged to form another string in JavaScript
- How to count the occurrence of a specific string in a string in JavaScript
- Remove characters from a string contained in another string with JavaScript?
- Twice repetitive word count in a string - JavaScript
- Inserting string at position x of another string using Javascript
- How to count a number of words in given string in JavaScript?
- Count number of occurrences for each char in a string with JavaScript?
- Finding the longest consecutive appearance of a character in another string using JavaScript
- How to count the number of occurrences of a character in a string in JavaScript?

Advertisements