Reputation: 27689
How to match end of line/word or not a digit
/\d{3}[\b\D]/
123
123-
123 1abc
123a
1234
Upvotes: 0
Views: 65
Reputation: 781300
If you want to match exactly 3 digits that aren't surrounded by any other digits, use negative lookarounds.
/(?<!\d)\d{3}(?!\d)/
^ ^
lookbehind lookahead
Lookarounds are explained at regular-expression.info.
Upvotes: 1
Reputation: 785286
You can use word boundary at start and a negative lookahead regex after 3 digits:
\b\d{3}(?!\d)
(?!\d)
is zero width assertion that will also allow a word boundary or end of line or any other non-digit after 3 digits.
Upvotes: 1