匹配&在Javascript中替换每个“第二次”出现的“

时间:2017-11-09 15:00:34

标签: javascript node.js str-replace

我想将“,(DoubleQuote逗号)”的每一次出现替换为“。”(点)

E.g。

“尽我的意愿,但更多的是反对我的心”, “我们现在两个人”, “我非常亲爱的”, “我们的安慰是,悲伤的道路如此清晰”,

应该是:

  "With all my will, but much against my heart",
  "We two now part.
  "My Very Dear",
  "Our solace is, the sad road lies so clear.

1 个答案:

答案 0 :(得分:3)

尝试使用split后跟reduce,如下所示

<强>演示

&#13;
&#13;
var input = `"With all my will, but much against my heart",
  "We two now part",
  "My Very Dear",
  "Our solace is, the sad road lies so clear",`
console.log( input.split( /",/ ).reduce( (a,b, i) => i %2 == 0 ? a + "\"," + b : a + "." + b ) );

console.log("It does the trick but it starts from the first line!");
&#13;
&#13;
&#13;