A
A
Artyom2020-07-07 09:06:26
JavaScript
Artyom, 2020-07-07 09:06:26

How to replace matches in regexp?


Everything is in this fiddle. There, my regular expression finds all the variables inserted into the string, and now I need to replace everything that it found with the values ​​of real variables, what is the easiest way to do this? Since in my last question on stackoverflow everyone wrote using the template string, I’ll immediately say that it is necessary for the admin panel to write such expressions there and variables were substituted in them

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-07-07
@airman66

const replacements = {
  worksAmount: 187,
  reviewsAmount: 666,
};

const newStr = str.replace(/\$\$\[(\w+)\]/g, (m, g1) => replacements[g1] ?? m);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question