Reputation: 71
Trying to replace substring inside double braces to a different string. For example, let's say string
a = "In order to unlock this attempt, you must contact your {{teacher}}. When the attempt is unlocked by your {{teacher}}, we will notify you!"
I want to convert a
to
"In order to unlock this attempt, you must contact your *coach*. When the attempt is unlocked by your *coach*, we will notify you!"
basically, replace "{{teach}}" to "Coach" in javascript. There are two or more to replace it. (problem)
Please how to replace using regular expressions (RegExp)it in javascript.
Thank you
Upvotes: 0
Views: 33
Reputation: 3302
Use String.prototype.replace()
method with regex. The replace method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced. Use the g
modifier to replace all the occurrences of the specified value. The syntax of replace method string.replace(searchValue, newValue)
const a = `In order to unlock this attempt, you must contact your {{teacher}}. When the attempt is unlocked by your {{teacher}}, we will notify you!`;
const ret = a.replace(/{{.*?}}/g, '*coach*');
console.log(ret);
Upvotes: 1