You can just replace every space and comma with space then trim those trailing spaces:
var str=" , this, is a ,,, test string , , to find regex,,in js. , ";
res = str.replace(/[, ]+/g, " ").trim();
jsfiddle demo
Answer from Jerry on Stack OverflowYou can just replace every space and comma with space then trim those trailing spaces:
var str=" , this, is a ,,, test string , , to find regex,,in js. , ";
res = str.replace(/[, ]+/g, " ").trim();
jsfiddle demo
you can use reg ex for this
/[,\s]+|[,\s]+/g
var str= "your string here";
//this will be new string after replace
str = str.replace(/[,\s]+|[,\s]+/g, 'your string here');
RegEx Explained and Demo
[javascript] how to effectively convert string to array while removing commas and the space at the beginning/end if there was any?
How to remove all commas from a string and add white space?
Removing commas in a string
Remove commas from the string using JavaScript - Stack Overflow
i think this can be done by regEx by i don't how to look it up.
this is a learning project, but i am just thinking about the scale
i have a form where the user enter a bunch of categories and i want the user to separate those categories with a comma, but working on the case where the user add the comma but also a space after the comma (as we all do) or before the comma, how to go about treating this case, because i don't want to end up with two or three categories that are the same.
edit: i did it but i don't want to remove the post to help anyone with the same issue.
here's what i did
const categoriesAsString = e.target.value;
const categoriesTrimmed = categoriesAsString.trim();
const categoriesAsStringWithWhiteSpace = categoriesTrimmed.replace(/\s*,\s*/g,",");
const categoriesAsArray = categoriesAsStringWithWhiteSpace.split(",");
setCategories(categoriesAsArray);
To replace all occurrences of a string with another string, using the following function:
str.replaceAll(',', '');
The split() method is not needed since you are not trying to turn the string into an array.
Just to remove all commas straighforwardly:
a.replace(',', '');
b.replace(',', '');
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
}
Use this regex /[.,\s]/g
var str = 'abc abc a, .aa ';
var regex = /[.,\s]/g;
var result = str.replace(regex, '');
console.log(result);
You don't need to escape character except ^-]\ in character class []
Any character except ^-]\ add that character to the possible matches for the character class.
I believe this should do it:
str.replace(/[.,\s]/g, '');
Here's a pretty simple & straightforward way to do this without needing a complex regular expression.
var str = " a , b , c ";
var arr = str.split(",").map(function(item) {
return item.trim();
});
console.log(arr)
Run code snippetEdit code snippet Hide Results Copy to answer Expand
The native .map is supported on IE9 and up: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Or in ES6+ it gets even shorter:
var str = " a , b , c ";
let arr = str.split(",").map(item => item.trim());
console.log(arr)
Run code snippetEdit code snippet Hide Results Copy to answer Expand
ES6 shorthand:
str.split(',').map(item=>item.trim())