สมมติว่าต่อไปนี้เป็นสตริงของเราที่มีเครื่องหมายจุลภาค -
"My Favorite subject is," "My Favorite subject is, and teacher name is Adam Smith" "My Favorite subject is, and got the marks 89"
หากต้องการแทนที่เครื่องหมายจุลภาค ให้ใช้การแทนที่ และใช้นิพจน์ทั่วไป ต่อไปนี้เป็นรหัส -
ตัวอย่าง
const makingRegularExpression = /,(?=[^,]*$)/;
replaceComma("My Favorite subject is,");
replaceComma("My Favorite subject is, and teacher name is Adam Smith");
replaceComma("My Favorite subject is, and got the marks 89");
function replaceComma(values){
console.log(values, " ==== replaced by JavaScript ==== ", values.replace(ma
kingRegularExpression, " JavaScript"));
} ในการรันโปรแกรมข้างต้น คุณต้องใช้คำสั่งต่อไปนี้ -
node fileName.js.
ที่นี่ ชื่อไฟล์ของฉันคือ demo164.js
ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
PS C:\Users\Amit\javascript-code> node demo164.js My Favorite subject is, ==== replaced by JavaScript ==== My Favorite sub ject is JavaScript My Favorite subject is, and teacher name is Adam Smith ==== replaced by JavaScript ==== My Favorite subject is JavaScript and teacher name is Ad am Smith My Favorite subject is, and got the marks 89 ==== replaced by JavaScript ==== My Favorite subject is JavaScript and got the marks 89