Reputation: 11731
I have a regular expression which validates 3 consecutive digits.
/^\d{3}$/.test("12345") // false
/^\d{3}$/.test("123") // true
How can I transform this regex pattern into a RegExp
object?
I tried:
var re = new RegExp("\\d{3}", "gi");
but re.test("12345")
returns true
What am I doing wrong?
Upvotes: 1
Views: 2120
Reputation: 74176
var re = new RegExp("^\\d{3}$", "gi");
(I assume the "gi" flag is not really necessary in this case...)
Upvotes: 5
Reputation: 56182
Use this regular expression:
^\d{3}$
with start and end of line specified.
In JavaScript you should escape \
char, i.e.:
"^\\d{3}$"
Upvotes: 2