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

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

    The "g" is a flag say says replacements should be made "globally". The default behavior is to only replace the first match.

    The use of the "g" flag for this purpose and the syntax of placing it right after a /-delimited regex comes from ed (and also appears in ex, sed, vi, etc.).

提交回复
热议问题