Tutorial :Using Regular Expressions with Javascript replace method



Question:

Friends,

I'm new to both Javascript and Regular Expressions and hope you can help!

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.

e.g.

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!

Thanks in advance for any help provided.

strong text Edit strong text

Thanks all for the input so far and the 3 answers given. Unfortunately I don't think I explained my question well enough.

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

Apologies for the confusion!


Solution:1

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');  


Solution: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.


Solution:3

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:

^,|,,|,$  


Solution:4

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, ""));  


Solution:5

// 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"]  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »