How do I do global string replace without needing to escape everything?

前端 未结 4 605
不思量自难忘°
不思量自难忘° 2021-01-23 21:02

I want to replace all occurences of a pattern in a string by another string. For example, lets convert all \"$\" to \"!\":

\"$$$\" -> \"!!!\"
<
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-23 21:33

    try this:

    function replaceAll(txt, replace, with_this) {
      return txt.replace(new RegExp(replace, 'g'),with_this);
    }
    

提交回复
热议问题