Vikrant Chaudhary
Vikrant Chaudhary

Reputation: 11319

Writing a Regular Expression for not including something

How can I write a Regular Expression to match,

    a string which does not contain the underscore "_".

Upvotes: 1

Views: 9199

Answers (2)

Paige Ruten
Paige Ruten

Reputation: 176803

To match a character that is not an underscore you'd use [^_] (the ^ means "not"). So to match a whole string you'd do something like this:

/[^_]+/

Upvotes: 0

Rob Di Marco
Rob Di Marco

Reputation: 45002

/^[^_]*$/

The [^] syntax means "do not include any of these characters".

Upvotes: 10

Related Questions