0

I don't have the idea about regex and how to match it with javascript.

I want to validate the string has:-

Min: 0A1

Max: 10J10

like: /10|[0-9][A-J]10|[1-10]/

But it is not working.

Thanks in Advance.

GAJESH PANIGRAHI
  • 1,204
  • 10
  • 17

2 Answers2

4

This pattern [1-10] matches a range from 1-1 or 0.

You could use an alternation that matches [0-9] (or [1-9] for the second case) or 10:

^(?:10|[0-9])[A-J](?:10|[1-9])$

Regex demo

let pattern = /^(?:10|[0-9])[A-J](?:10|[1-9])$/;
strings = [
  "0A1",
  "10J10",
  "6A10",
  "11J10"
].forEach(s => {
  console.log(s + " ==> " + pattern.test(s));
});
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
1

Try this :

/([0-9]|10)[A-J]([1-9]|10)/

In your regex :

[1-10] is wrong : is the same that : any caracters between 1 and 1 or 0
Sau
  • 35
  • 5