Để so khớp nhiều lần xuất hiện trong một chuỗi, hãy sử dụng biểu thức chính quy. Sau đây là mã -
Ví dụ
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)); Để chạy chương trình trên, bạn cần sử dụng lệnh sau -
node fileName.js.
Đây, tên tệp của tôi là demo70.js.
Đầu ra
Điều này sẽ tạo ra kết quả sau -
PS C:\Users\Amit\JavaScript-code> node demo70.js This is my first JavaScript Program which is the subset of typescript [ 'JavaScript', 'typescript' ]