replace '\'n in javascript

前端 未结 8 765
情深已故
情深已故 2020-12-17 09:10

I\'m trying to do replace in JavaScript using:

r = \"I\\nam\\nhere\";
s = r.replace(\"\\n\",\" \");

But instead of giving me

8条回答
  •  無奈伤痛
    2020-12-17 09:41

    use s = r.replace(/\\n/g," ");

    Get a reference:

    The "g" in the javascript replace code stands for "greedy" which means the replacement should happen more than once if possible

提交回复
热议问题