उदाहरण
रेगुलर एक्सप्रेशन और replace() से लास्ट कॉमा को रिप्लेस करें।
const makingRegularExpression = /,(?=[^,]*$)/;
function replaceComma(values) {
console.log(values, " ==== replaced by JavaScript ==== ", values.replace(makingRegularExpression, " JavaScript"));
}
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");
प्रोग्राम चलाने के लिए:
node fileName.js
आउटपुट
PS C:\Users\Amit\javascript-code> node demo164.js My Favorite subject is, ==== replaced by JavaScript ==== My Favorite subject is JavaScript My Favorite subject is, and teacher name is Adam Smith ==== replaced by JavaScript ==== My Favorite subject is JavaScript and teacher name is Adam Smith My Favorite subject is, and got the marks 89 ==== replaced by JavaScript ==== My Favorite subject is JavaScript and got the marks 89