Why do i need to add /g when using string replace in Javascript?

后端 未结 5 1089
走了就别回头了
走了就别回头了 2021-01-31 02:53

Why is the \'/g\' required when using string replace in JavaScript?

e.g. var myString = myString.replace(/%0D%0A/g,\"
\");

5条回答
  •  没有蜡笔的小新
    2021-01-31 02:59

    It isn't required, but by default string.replace in JavaScript will only replace the first matching value it finds. Adding the /g will mean that all of the matching values are replaced.

提交回复
热议问题