0

In Typescript, how to validate string pattern? example const str = "ABC1-AB9"

  1. max length should be 8
  2. string pattern should be xxxx-xxx
  3. x can be A-Z and 0-9 only
smac89
  • 39,374
  • 15
  • 132
  • 179
Nithin
  • 483
  • 5
  • 11
  • Have you try regex? [How to define a regex-matched string type in Typescript?](https://stackoverflow.com/questions/51445767/how-to-define-a-regex-matched-string-type-in-typescript) – J L Sep 01 '22 at 05:20
  • 4
    do you want a regex to validate it or a type? – nullptr Sep 01 '22 at 05:28
  • 1
    This is either a duplicate of [this question](https://stackoverflow.com/q/51445767/438273) or you can use a regular expression [like this](https://regexr.com/6t3dl): `/^[0-9A-Z]{4}-[0-9A-Z]{3}$/` – jsejcksn Sep 01 '22 at 06:10

1 Answers1

0
^[A-z0-9]{4}-[A-z0-9]{3} [0-9]{1}

This could be the regex you are looking for. You can test the same by going to https://regexr.com/ website and making your own custom regex.

Bhavesh
  • 74
  • 7