Within a JavaScript function I need to check to see if a comma (,) appears 1 or more times. If it does then there should be one or more numbers either side of it.
Examples:
- 1,000.00 is ok
- 1,000,00 is ok
- ,000.00 is not ok
- 1,,000.00 is not ok
If these conditions are met I want the comma to be removed so 1,000.00 becomes 1000.00
What I have tried so is:
var x = '1,000.00';
var regex = new RegExp("[0-9]+,[0-9]+", "g");
var y = x.replace(regex,"");
alert(y);
When run the alert shows ".00" Which is not what I was expecting or want!
What I am trying to achieve is:
If there is a comma in the text and there are one or more numbers either side of it then remove the comma but leave the rest of the string as is.
If there is a comma in the text and there is not at least one number either side of it then do nothing.
So using my examples from above:
- 1,000.00 becomes 1000.00
- 1,000,00 becomes 100000
- ,000.00 is left as ,000.00
- 1,,000.00 is left as 1,,000.00