Reputation: 3491
I need a regular expression pattern that matches any number including 1-9 numbers except 2?
My attempt:
([1-9][^2])
But this doesn't work for me.
Upvotes: 14
Views: 16305
Reputation: 6414
Another way to do it:
/[^\D2]/
Which means, not a non-digit or 2.
Upvotes: 23
Reputation: 12027
You can match the range of numbers before and after two with [0-13-9]
, like this:
"4526".match(/[0-13-9]+/)
["45"]
"029".match(/[0-13-9]+/)
["0"]
"09218".match(/[0-13-9]+/)
["09"]
Upvotes: 15