How about doing the test.replace with a negative lookahead - https://regex101.com/r/WtHcuO/2/:
var data = JSON.stringify([{"var1":"0","description":"this, has, commas"},{"var1":"1","description":"more, commas"}]);
var stripped = data.replace(/,(?!["{}[\]])/g, "");
console.log(stripped);
Or, if you want to preserve the commas, but escape them, you can replace with \\, instead of ""
var data = JSON.stringify([{"var1":"0","description":"this, has, commas"},{"var1":"1","description":"more, commas"}]);
var stripped = data.replace(/,(?!["{}[\]])/g, "\\,");
console.log(stripped);
Answer from combatc2 on Stack Overflowjavascript - JSON Remove trailing comma from last object - Stack Overflow
javascript - How to remove commas from json object displayed in HTML? - Stack Overflow
Remove commas in numbers for JSON output
Remove comma from number in array.
You need to find ,, after which there is no any new attribute, object or array.
New attribute could start either with quotes (" or ') or with any word-character (\w).
New object could start only with character {.
New array could start only with character [.
New attribute, object or array could be placed after a bunch of space-like symbols (\s).
So, the regex will be like this:
const regex = /\,(?!\s*?[\{\[\"\'\w])/g;
Use it like this:
// javascript
const json = input.replace(regex, ''); // remove all trailing commas (`input` variable holds the erroneous JSON)
const data = JSON.parse(json); // build a new JSON object based on correct string
Try the first regex.
Another approach is to find every ,, after which there is a closing bracket.
Closing brackets in this case are } and ].
Again, closing brackets might be placed after a bunch of space-like symbols (\s).
Hence the regexp:
const regex = /\,(?=\s*?[\}\]])/g;
Usage is the same.
Try the second regex.
Consider the Json input = [{"ITEM1":{"names":["nameA"]}},{"ITEM2":{"names":["nameB","nameC"]}},] without whitespaces. I suggest a simple way using substring.
input = input.substring(0, input.length-2);
input = input + "]";
if you just want to remove the commas:
systemList[0].comments.join("");
if you want to add space in between values:
systemList[0].comments.join(" ");
if you want to add anything in between values:
systemList[0].comments.join("anything");
reference for using .join() function:
https://www.w3schools.com/jsref/jsref_join.asp
I would prefer using one of this two methods:
var text = ',,,TEST STATUS GREEN,,TEST STATUS GREEN,';
var result1 = text.replace(/,/g,'')
console.log(result1)
var result2 = text.split(',').join('')
console.log(result2)
Greetings :)
Hi all, I'm trying to remove commas from my numbers that are in an array :
I know the method '.toFixed(2)' but I'm having trouble applying it in a .map
This is what I did:
And this is what I get :
Unfortunately I added the two values so I don't get an array anymore.
What would be the right way to find an array like I had but without a comma?
How do I merge them into my array? 🙂
You can simplify the regular expression:
num.replace(/,/g, '')
Replace the regex in the replace method with /,/g which means matches the character,literally (case sensitive)
var num = '12,312,313,214,214,324.89';
var num2 = '12,312,313,214,214,324';
function replaceComma(num) {
return num.replace(/,/g, '');
};
console.log(replaceComma(num));
console.log(replaceComma(num2));
Run code snippetEdit code snippet Hide Results Copy to answer Expand
To remove the commas, you'll need to use replace on the string. To convert to a float so you can do the maths, you'll need parseFloat:
var total = parseFloat('100,000.00'.replace(/,/g, '')) +
parseFloat('500,000.00'.replace(/,/g, ''));
Related answer, but if you want to run clean up a user inputting values into a form, here's what you can do:
const numFormatter = new Intl.NumberFormat('en-US', {
style: "decimal",
maximumFractionDigits: 2
})
// Good Inputs
parseFloat(numFormatter.format('1234').replace(/,/g,"")) // 1234
parseFloat(numFormatter.format('123').replace(/,/g,"")) // 123
// 3rd decimal place rounds to nearest
parseFloat(numFormatter.format('1234.233').replace(/,/g,"")); // 1234.23
parseFloat(numFormatter.format('1234.239').replace(/,/g,"")); // 1234.24
// Bad Inputs
parseFloat(numFormatter.format('1234.233a').replace(/,/g,"")); // NaN
parseFloat(numFormatter.format('$1234.23').replace(/,/g,"")); // NaN
// Edge Cases
parseFloat(numFormatter.format(true).replace(/,/g,"")) // 1
parseFloat(numFormatter.format(false).replace(/,/g,"")) // 0
parseFloat(numFormatter.format(NaN).replace(/,/g,"")) // NaN
Use the international date local via format. This cleans up any bad inputs, if there is one it returns a string of NaN you can check for. There's no way currently of removing commas as part of the locale (as of 10/12/19), so you can use a regex command to remove commas using replace.
ParseFloat converts the this type definition from string to number
If you use React, this is what your calculate function could look like:
updateCalculationInput = (e) => {
let value;
value = numFormatter.format(e.target.value); // 123,456.78 - 3rd decimal rounds to nearest number as expected
if(value === 'NaN') return; // locale returns string of NaN if fail
value = value.replace(/,/g, ""); // remove commas
value = parseFloat(value); // now parse to float should always be clean input
// Do the actual math and setState calls here
}