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

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

    The g regular expression modifier (called the global modifier) basically says to the engine not to stop parsing the string after the first match. If you were to omit the modifier, only the first instance of %0D%0A would be replaced (it might be desirable in some cases).

提交回复
热议问题