You can use the following regex defined with the help of a RegExp
constructor notation (so as to avoid escaping /
):
let regCode = new RegExp('/\\d{10,11}$')
Or, with a regex literal (to avoid escaping backslashes twice):
let regCode = /\/\d{10,11}$/
Surely, you can also use [0-9]
instead of \\d
in the first statement to avoid the "backslash hell".
Details:
/
- a /
char
\d{10,11}
- ten or eleven digits
$
- end of string.
See the regex demo.
Note the absence of the g
lobal modifier in the regex, see Why does a RegExp with global flag give wrong results? to understand why.
See a JavaScript demo:
let regCode = new RegExp('/\\d{10,11}$');
console.log("test/1234567890 =>", regCode.test("test/1234567890"))
console.log("test/1234567890abc =>", regCode.test("test/1234567890abc"))
console.log("test/abc1234567a890abc =>", regCode.test("test/abc1234567a890abc"))