Javascript replace regex don't work -


this question has answer here:

i have problem regexp replace html code in string. had tried online regexp tester match, causes canť replace. ${productean} want replace. idea wrong?

the string here:

<label for="productean" id="productean" class="col-sm-3 control-label">${productean}</label> 

javascript code:

let strtoreplace = 'productean'; let reg = new regexp('/(\$\{'+strtoreplace+'\})/','g')         str.replace(reg,'some text'); 

the regexp constructor doesn't need slashes (/.../) around expression, regex literals. , when backslash-escape characters, escaped in string passed constructor, pass unescaped actual regex expression. need double-escaped. so, change line this:

let reg = new regexp('\\$\\{'+strtoreplace+'\\}','g'); 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -