Scott Klarenbach
Scott Klarenbach

Reputation: 38721

Javascript Regex Ensure Last 3 characters are digits?

How can I validate that a string ends with exactly three digits?

valid:

Hey-12-Therexx-111
001
xxx-5x444

invalid:

Hey-12-Therexx-1111
Hey-12-Therexx-11
Hey-12-Therexx
12
112x

Upvotes: 2

Views: 985

Answers (1)

ruakh
ruakh

Reputation: 183251

You can write:

/(^|\D)\d{3}$/

which means, "start-of-string-or-non-digit-character, followed by three digits, followed by end-of-string".

Upvotes: 9

Related Questions