2

I want to replace every second occurrence of ", (DoubleQuote comma) with "." (Dot)

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

Should be:

  "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.
Ajinkya Bapat
  • 619
  • 1
  • 10
  • 26

1 Answers1

4

Try using split followed by reduce as shown below

Demo

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!");
Ajinkya Bapat
  • 619
  • 1
  • 10
  • 26
gurvinder372
  • 66,980
  • 10
  • 72
  • 94