0

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
2
  • What's wrong with just removing all commas within the string... E.g. String.replace(',',''); Commented Apr 20, 2009 at 10:56
  • Thanks for the comment Jimmy. That was my first solution too! Unfortunately some of the fields allow comma's to be included as part of the formatting. So before I let the user leave the field and they have entered a comma then there must be at least a number either side. Basically it to stop users entering 1,,,,,,,,,,,,,,,,000 etc Commented Apr 20, 2009 at 11:22

5 Answers 5

4

Your regex isn't going to be very flexible with higher orders than 1000 and it has a problem with inputs which don't have the comma. More problematically you're also matching and replacing the part of the data you're interested in!

Better to have a regex which matches the forms which are a problem and remove them.

The following matches (in order) commas at the beginning of the input, at the end of the input, preceded by a number of non digits, or followed by a number of non digits.

var y = x.replace(/^,|,$|[^0-9]+,|,[^0-9]+/g,'');

As an aside, all of this is much easier if you happen to be able to do lookbehind but almost every JS implementation doesn't.

Edit based on question update:

Ok, I won't attempt to understand why your rules are as they are, but the regex gets simpler to solve it:

var y = x.replace(/(\d),(\d)/g, '$1$2');
Sign up to request clarification or add additional context in comments.

2 Comments

Sure, good point, but the OP would need to do both lookahead and back. You could benchmark it if it was important, but I think multiple operations would be clunkier than the regex above.
Hi annakata, Thanks for the solutions - the second one was exactly what I needed. Thanks again! Regards Ian
2

I would use something like the following:

^[0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)$
  • [0-9]{1,3}: 1 to 3 digits
  • (,[0-9]{3})*: [Optional] More digit triplets seperated by a comma
  • (\.[0-9]+): [Optional] Dot + more digits

If this regex matches, you know that your number is valid. Just replace all commas with the empty string afterwards.

1 Comment

Nice answer, start by checking the correctness of your number. Once this is done, you just have to remove every commas...
1

It seems to me you have three error conditions

  1. ",1000"
  2. "1000,"
  3. "1,,000"

If any one of these is true then you should reject the field, If they are all false then you can strip the commas in the normal way and move on. This can be a simple alternation:

^,|,,|,$

Comments

1

I would just remove anything except digits and the decimal separator ([^0-9.]) and send the output through parseFloat():

var y = parseFloat(x.replace(/[^0-9.]+/g, ""));

Comments

1
// invalid cases:
// - standalone comma at the beginning of the string
// - comma next to another comma
// - standalone comma at the end of the string
var i,
    inputs = ['1,000.00', '1,000,00', ',000.00', '1,,000.00'],
    invalid_cases = /(^,)|(,,)|(,$)/;
for (i = 0; i < inputs.length; i++) {
    if (inputs[i].match(invalid_cases) === null) {
       // wipe out everything but decimal and dot
       inputs[i] = inputs[i].replace(/[^\d.]+/g, '');
    }
}
console.log(inputs); // ["1000.00", "100000", ",000.00", "1,,000.00"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.