Multiple simultaneous replaces with javascript [closed]

0

How can I do multiple simultaneous replicates without having to do one by one?

Example: I have a formula, in a string format, whose image follows immediately.

IhavealreadycalculatedallthesevaluesandIhavetoreplacethesecalculatedvaluesinUSERS.

Youhaveinsteadofdoingformula=formula.replace(totalUsers,"USERS"); one by one, make all these values simultaneous?

    
asked by anonymous 20.09.2017 / 16:34

1 answer

2

Replace only replaces the first occurrence if you receive a String as the first argument. If you use a regex with falg g it replaces all that it finds. I think that's what you're looking for:

const regex = new RegExp(totalUsers, 'g'); // g é para multiplas ocurrências
formula = formula.replace(regex, "USERS");
    
20.09.2017 / 16:38