skip to Main Content

I need to remove thousand comma separator (,) in Total Chargeable column. Currently i’m using
line = line.replace(',', ''); but then it will remove all commas.

Any suggestions how we can remove thousand comma without removing separator comma in JavaScript?

File is csv format but view on notepad

Example :

As-Is

"Modem ID","Total Chargeable"
"BF002062010","1,300.00"
"BF002062013","1,900.00"
"VV878443","1,000.00"
"","2,200.00"
"BF002061500","5,300.67"
"BF002062151","2,000.00"

To-Be

"Modem ID","Total Chargeable"
"BF002062010","1300.00"
"BF002062013","1900.00"
"VV878443","1000.00"
"","2200.00"
"BF002061500","5300.67"
"BF002062151","2000.00"

5

Answers


  1. Add back the comma.

    line.replaceAll(',', '').replace('""', '","')
    
    Login or Signup to reply.
  2. First, you can replace "," to another character. For example, you can change "BF002062010","1,300.00" to "BF002062010"-"1,300.00".

    Then, using line = line.replace(',', ''); removes thousand comma.

    Finally, you can replace "-" to replace ",".

    Login or Signup to reply.
  3. Or, maybe you could use some CSV reader library to looping through the CSV contents like this?

    const fs = require('fs');
    const csv = require('csv-parser');
    const writer = require('csv-writer').createObjectCsvWriter;
    
    
    const rows = [];
    
    fs.createReadStream('test.csv')
      .pipe(csv())
      .on('data', (row) => {
          let temp = {};
          temp['Modem ID'] = row['Modem ID']
          temp['Total Chargeable'] = row['Total Chargeable'].replace(',', '');
    
          rows.push(temp);
    
      })
      .on('end', () => {
          console.log('CSV file successfully processed');
          const csvWriter = writer({
            path: 'output.csv',
            header: Object.keys(rows[0]).map(header => ({ id: header, title: header }))
        });
        csvWriter.writeRecords(rows)
        .then(() => {
            console.log('CSV file successfully written with updated data');
        })
      });
    
    Login or Signup to reply.
  4. This should work for your case:

    const data = `
    "Modem ID","Total Chargeable"
    "BF002062010","1,300.00"
    "BF002062013","1,900.00"
    "VV878443","1,000.00"
    "","2,200.00"
    "BF002061500","5,300.67"
    "BF002062151","2,000.00"
    `;
    
    const output = data.replace(/".*?"/g, g => g.replaceAll(',', ''));
    
    console.log(output);
    Login or Signup to reply.
  5. We can try the following regex replacement approach:

    var input = `"Modem ID","Total Chargeable"
    "BF002062010","1,300.00"
    "BF002062013","1,900.00"
    "VV878443","1,000.00"
    "","2,200.00"
    "BF002061500","5,300.67"
    "BF002062151","2,000.00"`;
    
    var output = input.replace(/".*?"|,/g, (x) => x === "," ? "," : x.replace(",", ""));
    console.log(output);

    The regex pattern used here says to match:

    • ".*?" first try to match at term in double quotes
    • | OR
    • , that failing, match a comma

    Then, we use a callback function to selectively strip commas only from the numbers in double quotes, leaving the comma separators alone.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search