简体   繁体   中英

How to replace all string with dollar sign in html file?

I get html template as string from db and before i write file in node I want to replace all ${survey_url} with number in this string. I tried with:

str.replace(/${survey_url}/g, params.orderId)

but it does not work. Could you help me understanding what's wrong?

{ , } and $ are a meaningful characters in a regular expression, so you'll need to escape them.

str.replace(/\$\{survey_url\}/g, params.orderId);

MDN has a good section on how regular expressions work .

The problem is that $ and {...} are special characters, so they aren't interpreted literally.

Particularly:

  • $ means the end of a string
  • {...} indicates a range

So you need to escape these characters:

 const output = '${greeting} user!'.replace(/\\$\\{greeting\\}/g, 'hello'); console.log(output); 

您可以尝试逃避$\\$

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