Match multiple occurrences in a string with JavaScript?


To match multiple occurrences in a string, use regular expressions. Following is the code −

Example

function checkMultipleOccurrences(sentence) {
   var matchExpression = /(JavaScript?[^\s]+)|(typescript?[^\s]+)/g;
   return sentence.match(matchExpression);
}
var sentence="This is my first JavaScript Program which is the subset
of typescript";
console.log(sentence);
console.log(checkMultipleOccurrences(sentence));

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo70.js.

Output

This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo70.js
This is my first JavaScript Program which is the subset of typescript
[ 'JavaScript', 'typescript' ]

Updated on: 07-Sep-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements