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

前端 未结 4 618
不思量自难忘°
不思量自难忘° 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:16

    You can give the regest as a string and provide a third argument for the flags you want

    https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace

    "$$$".replace("$", "!", "g");
    

提交回复
热议问题