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

后端 未结 5 1053
走了就别回头了
走了就别回头了 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 03:18

    The 'g' flag means "global" so each occurrence of %0D%0A will be replaced. Otherwise it would replace the FIRST occurrence only.

提交回复
热议问题