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
Add back the comma.
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","
.Or, maybe you could use some CSV reader library to looping through the CSV contents like this?
This should work for your case:
We can try the following regex replacement approach:
The regex pattern used here says to match:
".*?"
first try to match at term in double quotes|
OR,
that failing, match a commaThen, we use a callback function to selectively strip commas only from the numbers in double quotes, leaving the comma separators alone.