regex - How to match the exact number of digits with regular expression in Javascript -
in javascript, have done following:
var matched = searched.match(/\d{7}/)
it works great if searched
1234567
or xyz1234567
. both return 1234567
, good. 123
, xyz123
return null, expected.
but 1 condition fails when searched
12345678
or xyz12345678
. both return null because exact 7 digit match. both return 2345678
instead.
/\d{7}$/
not work either.
can please advise?
thank you
(?:\d|^)
: begins non-digit
(?:\d|$)
: ends non-digit
var matched = searched.match(/(?:\d|^)(\d{7})(?:\d|$)/); if (matched) { matched = matched[1]; }
Comments
Post a Comment