[Solved] Regex pattern in Javascript


Ramesh, does this do what you want?

^[a-zA-Z0-9-]{4}\|[a-zA-Z0-9-]{4}\|[a-zA-Z0-9-]{7,}$

You can try it at https://regex101.com/r/jilO6O/1

For example, the following will be matched:

  • test|test|test123
  • a1-0|b100|c10-200
  • a100|b100|c100200

But the following will not:

  • a10|b100|c100200
  • a100|b1002|c100200
  • a100|b100|c10020

Tips on modifying your original code.

You have “a-za-z” where you probably intended “a-zA-Z”, to allow either upper or lower case.

To specify the number of characters to be exactly 4, use “{4}”. You were nearly there with your round brackets, but they need to be curly, to specify a count.

To specify a range of number of characters, use “{lowerLimit,upperLimit}”. Leaving the upper limit blank allows unlimited repeats.

We need to escape the “|” character because it has the special meaning of “alternate”, in regular expressions, i.e. “a|b” matches either “a” or “b”. By writing it as “\|” the regex interpreter knows we want to match the “|” character itself.

0

solved Regex pattern in Javascript