Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 857
Scott Klarenbach Avatar asked Dec 30 '25 20:12

Scott Klarenbach


1 Answers

You can write:

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

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

like image 118
ruakh Avatar answered Jan 01 '26 10:01

ruakh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!