Javascript replace regex don't work -
this question has answer here:
- regular expression [allchars] 1 answer
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
Post a Comment