คุณสามารถใช้การจับคู่ () โดยส่งชื่อตัวแปร ไวยากรณ์มีดังนี้ −
console.log(yourVariableName.match(yourMatchingVariableName));
ตัวอย่าง
var senetence = 'My Name is John';
console.log("The actual value=");
console.log(senetence);
var matchWord = 'John';
console.log("The matching value=");
console.log(matchWord);
var matchRegularExpression = new RegExp(matchWord, 'g' );
console.log(senetence.match(matchWord)); ในการรันโปรแกรมข้างต้น คุณต้องใช้คำสั่งต่อไปนี้ -
node fileName.js.
ที่นี่ ชื่อไฟล์ของฉันคือ demo107.js
ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
PS C:\Users\Amit\JavaScript-code> node demo107.js The actual value= My Name is John The matching value= John