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 remove all commas from a string and add white space? - Stack Overflow
regex - JavaScript remove fullstop, comma and spaces from a string - Stack Overflow
Removing commas in a string
javascript - Remove spaces and commas at the end and beginning of comma separated string - 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(',', '');
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, '');
That should work:
str = str.replace(/\s*,\s*/g, ",");
var str = " I would like to know how to use RegExp , string.match and string.replace";
console.log(
str
);
console.log(
str
//Replace double space with single
.replace(/ +/ig, ' ')
);
console.log(
str
//Replace double space with single
.replace(/ +/ig, ' ')
//Replace any amount of whitespace before or after a `,` to nothing
.replace(/\s*,\s*/ig, ',')
);
Split on any sequence of spaces and commas:
str.split(/[ ,]+/).join(',')
You might also want to use filter to remove empty strings:
str.split(/[ ,]+/).filter(function(v){return v!==''}).join(',')
Another solution would be to match any sequence that does not contain a space or comma:
str.match(/[^ ,]+/g).join(',')
Use the String.replace() method.
var newString = yourString.replace(/[ ,]+/g, ",");
This says to replace any sequence of one or more spaces or commas in a row with a single comma, so you're covered for strings where the commas have spaces around them like "test, test,test test test , test".
(Note: if you want to allow for other whitespace characters beyond just a space use \s in the regular expression: /[\s,]+/g)
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
}