[Solved] javascript regex to validate the input


You can try ^(?!0\*0)(?:\d+\*\d+,?)+$.

  • ^ and $ ensure that the entire string matches.
  • (?!0\*0) is a negative look ahead to check that the string is not 0*0.
  • (?: and ) indicates that the contents are a non-capturing group
  • \d+ matches one or more digits (0–9)
  • \* matches a literal * character
  • \d+ matches one or more digits (0–9)
  • ,? optionally match a literal comma
  • + match one or more of the preceding pattern ((?:\d+\*\d+,?))

Known bug: 50*520*4 matches.

Edit: Found a workaround for the bug.

^(?:(?!0\*0)\d+\*\d+,)*(?:(?!0\*0)\d+\*\d+)$

Edit: You edited your question to make it clear that you do not want to match 50*5,0*0. This can be achieved by putting the negative lookahead into the repeated match as ^(?:(?!0\*0)\d+\*\d+,?)+$

Edit: You mentioned that you don’t want to match 0*[anything] and [anything]*0. Try ^(?:(?!0\*)\d+\*(?!0,)\d+,)*(?:(?!0\*)\d+\*(?!0$)\d+)$.

Play around with the latest regex with the workaround on this RegExr demo.

2

solved javascript regex to validate the input