0

I am trying pattern validation for my input field.

^(OTC[0-9]{1,99})(,OTC[0-9]{1,99})*$

For the above pattern following inputs are valid:

OTC123,OTC567,OTC987

but I also want to allow whitespaces after commas:

OTC123,   OTC567,   OTC987.

Any help is appreciated

  • So, just add `\s?` or `\s*` at those locations in the pattern where you want to "allow" whitespaces. – Wiktor Stribiżew May 19 '20 at 08:51
  • I tried ^(OTC[0-9]{1,99})(,\s*OTC[0-9]{1,99})*$ but did not work – Ajinkya Phand May 19 '20 at 08:54
  • You can use the \s flag for capture white space and the ? after \s? matches any whitespace character (equal to [\r\n\t\f\v ]) ? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy) try this one mate ```^(OTC[0-9]{1,99})(,\s?OTC[0-9]{1,99})*$``` – ShobiDobi May 19 '20 at 08:54
  • Even ^(OTC[0-9]{1,99})(,\s?OTC[0-9]{1,99})*$ i is not working – Ajinkya Phand May 19 '20 at 08:57
  • [**It works**](https://regex101.com/r/pt0cmk/1). `/^OTC[0-9]{1,99}(?:,\s*OTC[0-9]{1,99})*$/.test("OTC123, OTC567, OTC987")` works, too. – Wiktor Stribiżew May 19 '20 at 09:00
  • Well I solved the prob by just adding [ ]* to allow Whitespace...thnk you for ur help – Ajinkya Phand May 19 '20 at 09:04

0 Answers0