-1

I am going to use JavaScript to validate a string that needs to be:

  • Only digits and commas
  • Can't end or start with a comma
  • Each number between the commas has to less than 1,000
  • No negatives
  • No periods, just digits and commas

I tried:

\d{1,3}(,{1,3})*

But it doesn't quite do what I mentioned above.

2
  • Parhaps, it should be ` \d{1,3}(,\d{1,3})*? (the second \d` is missing). – Alexander Mashin Oct 3 '20 at 8:00
  • Questions on this site should be about usign web applications as it's defined in help center. What web application are you using? Questions about programming languages that arent' directly related to using a web application are off-topic here, try Stack Overflow. – Rubén Oct 3 '20 at 15:56
0

You can use the following regular expression to match numbers starting at new line, having comma separator between groups of 3 digits.

^(?:\d{1,3}(?:(,?)\d{3})?(?:\1\d{3})*)$

Details:

  • ^ asserts position at start of a line.
  • ?: Match expression but do not capture it.
  • \d matches a digit (equal to [0-9]).
  • {1,3} Quantifier — Matches between 1 and 3 times, as many times as possible, giving back as needed.
  • Capturing Group (,?) matches the character , literally.
  • \1 matches the same text as most recently matched by the 1st capturing group.
  • {3} Quantifier — Matches exactly 3 times.
  • $ asserts position at the end of a line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.