Javascript Replace with Global not working on Caret Symbol

后端 未结 2 1584
情话喂你
情话喂你 2021-01-24 01:22

i have following code for replacing

 var temp =  \"^hall^,^restaurant^\";
 temp.replace(/^/g, \'\');
 console.log(temp);

This does not Replace

2条回答
  •  无人共我
    2021-01-24 01:52

     temp = temp.replace(/\^/g, '');
    

    It is replacing once you escape the caret.

    https://jsfiddle.net/ym7tt1L8/

    And note that just writing temp.replace(/\^/g, ''); doesn't update your actual string. That is the reason you have to write

     temp = temp.replace(/\^/g, '');
    

提交回复
热议问题