Below is my string :
api/Node-1/{Node-1}/Node-1-1/{Node-1-1}
Search Term : Node-1
Replace with : Learning
Expected output :
api/Learning/{Learning}/Node-1-1/{Node-1-1}
Now here the problem is Node-1 is matching with other Node-1-1 also but I want exact word matching and replacement.
I have tried lots of options but none of them is working for me.
function replaceAll(str, find, replace) {
return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}
function escapeRegExp(str) {
return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}
console.log(replaceAll('api/Node-1/{Node-1}/Node-1-1/{Node-1-1}','Node-1','Learning'));
var replaceStr = 'Node-1';
console.log( 'api/Node-1/{Node-1}/Node-1-1/{Node-1-1}'.replace(new RegExp("\\b"+replaceStr+"\\b","gi"),"Learning"));
console.log( 'api/Node-1/{Node-1}/Node-1-1/{Node-1-1}'.replace(/\bNode-1\b/g,'Learning'));
Update : This question is not duplicate as my first answer is taken from this reference only which is not working with my input case.