简体   繁体   中英

Replace substring inside double braces to a different string in javascript

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM