【问题标题】:Match & Replace every "Second" occurrence of ", in Javascript在 Javascript 中匹配并替换“,”的每个“第二”出现
【发布时间】:2017-11-10 13:46:54
【问题描述】:

我想用“.”(点)替换每两次出现的“,(双引号逗号)

例如

“尽我的意愿,但非常违背我的心”, “我们两个现在分开”, “我亲爱的”, “我们的安慰是,悲伤的道路如此清晰”,

应该是:

  "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.

【问题讨论】:

标签: javascript node.js str-replace


【解决方案1】:

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

演示

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!");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 2022-01-06
    相关资源
    最近更新 更多