Match multiple occurrences in a string with JavaScript?

JavascriptWeb DevelopmentObject Oriented Programming

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' ]
raja
Published on 07-Sep-2020 07:14:12

Advertisements