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

javascriptweb developmentobject oriented programming更新于 2025/11/10 16:44:17

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

示例

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' ]

相关文章


有用资源