Max Koretskyi
Max Koretskyi

Reputation: 105449

match ascii characters except few characters

I have a regexp that matches all ascii characters:

/^[\x00-\x7F]*$/

Now I need to exclude from this range the following characters: ', ". How do I do that?

Upvotes: 6

Views: 3953

Answers (3)

deceze
deceze

Reputation: 522016

The IMO by far simplest solution:

/^[\x00-\x21\x23-\x26\x28-\x7F]*$/

Upvotes: 1

gurvinder372
gurvinder372

Reputation: 68393

You can exclude characters from a range by doing

/^(?![\.])[\x00-\x7F]*$/

prefixed it with (?![\.]) to exlude . from the regex match.

or in your scenario

/^(?!['"])[\x00-\x7F]*$/

Edit:

wrap the regex in braces to match it multiple times

/^((?!['"])[\x00-\x7F])*$/

Upvotes: 3

anubhava
anubhava

Reputation: 784988

You can use negative lookahead for disallowed chars:

/^((?!['"])[\x00-\x7F])*$/

RegEx Demo

(?!['"]) is negative lookahead to disallow single/double quotes in your input.

Upvotes: 5

Related Questions