An interview question from Didi, ask everyone how to solve it.

add the style of red font to the content in this string {{}}, and remove {{}}
const a ="

Please change these {{content}}, the font to {{red} < / div >"
take a closer look at the variables declared by const can"t be changed, is it wrong? Do you have a good solution

Dec.14,2021

a.replace(/{{([\s\S]*?)}}/g,'<p style="color:red">$1

')

just replace it with replace

const b = a.replace(/\{\{/g,'<span style="color:red">').replace(/\}\}/g,'</span>')

clipboard.png

then insert b into the page


does not require variables to be changed, it just requires processing string contents


The

title requires changing the value of the string. All you need to do is replace the string with a regular.
as for the const declaration you want to implement, you can do this

.
  var str="const a = '<div>{{}},{{}}</div>'";
  var reg = /\{\{(.*?)\}\}/g;
  str = str.replace(reg,'<b style="color:red">'+$1+'</b>');
  eval1(str);


function eval1 () {
    var script = document.createElement('script');
    script.type="text/javascript";
    script.text=str;
    document.getElementsByTagName('head')[0].appendChild(script);
}

a cannot be changed, but you can save it as variable B.
whether it is a constant or a variable, it is meant to be used.

Menu