使用JavaScript匹配字符串中的多次出现?

要匹配字符串中的多次出现,请使用正则表达式。以下是代码-

示例

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));

要运行上述程序,您需要使用以下命令-

node fileName.js.

在这里,我的文件名为demo70.js。

输出结果

这将产生以下输出-

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