-2

I want to write a regex which returns true if first n characters are numeric and only the last character is alphabet(single-capital-char.) and n could be any value.

What I have tried so far:

let str1 = "1222B";
console.log( /^[0-9][A-Z]$/.test(str1) ); // logs false but I'm expecting true.

For eg.

str1 = "1A"; -> true
str1 = "112111C"-> true
str1 = "11CC" -> false
str1 = "1c"-> false
Ivar
  • 6,138
  • 12
  • 49
  • 61

1 Answers1

-1

Read about quantifiers. Visit https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Quantifiers

let str1 = "1222B";
console.log( /^[0-9]+[A-Z]$/.test(str1) );