0

How could I replace comma inside double quote using nodejs or nodejs moudle like fs ?

input: 48,"LHT0019","TEST COUNT (HB,TW,DC,PL)"
output: 48,"LHT0019","TEST COUNT (HB TW DC PL)"
Himanshu Poddar
  • 7,112
  • 10
  • 47
  • 93
kkk
  • 95
  • 1
  • 2
  • 11

2 Answers2

0

Something like this?

'48,"LHT0019","TEST COUNT (HB,TW,DC,PL)"' // Your string
  .replace(
    /(["'])(?:(?=(\\?))\2.)*?\1/g, // REGEX to find text in quotes
    function(match, capture) { // Pass result to a function to modify further
      return match.replace(/\,/g, ' '); // REGEX to remove all ","
    }
  );

Uses REGEX to find and then modify the strings.

With a little help from RegEx: Grabbing values between quotation marks

Jason
  • 556
  • 1
  • 5
  • 21
0

You ca use str.replace as follows

var str  = "TEST COUNT (HB,TW,DC,PL)";
console.log("rrrr", str.replace(/,/g," "));

OUTPUT:

TEST COUNT (HB TW DC PL)

Subburaj
  • 5,114
  • 10
  • 44
  • 87
  • This will replace every comma in the string though which might lead to unintended consequences – Jorg Jan 23 '20 at 06:50