Mohammad Masoudian
Mohammad Masoudian

Reputation: 3491

Regex pattern that matches any number include 1-9 except 2

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

Answers (4)

nab1x9
nab1x9

Reputation: 31

Or this is also the correct answer.

/(?!2)\d/

Upvotes: 3

Stuart Wakefield
Stuart Wakefield

Reputation: 6414

Another way to do it:

/[^\D2]/

Which means, not a non-digit or 2.

Upvotes: 23

Prakash GPz
Prakash GPz

Reputation: 1523

This RegExp works: /([013-9])/

Upvotes: 1

Lepidosteus
Lepidosteus

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

Related Questions