pret
pret

Reputation: 273

javascript regex not to allow non ASCII and backslash

I am looking for a regular expression in JAVASCRIPT to allow a string which satisfies all the three below:

  1. Can allow all characters except the special character "\"
  2. Should not allow non ASCII characters

How can I get the regex for both of the above...

I have written regex as ^[0-9a-zA-Z' ']+$, but this is allowing non ASCII. I see online that \x00-\x7F allows non-ASCII chars, but how to combine both of these as a single regex?

Upvotes: 0

Views: 1121

Answers (1)

anubhava
anubhava

Reputation: 785196

This should work:

var match = str.match(/^(?:(?![^\x00-\x7F"]).)+$/);

Negative lookahead is used to to make sure each character is not non-ASCII OR ".

OR

var match = str.match(/^(?:(?!")[\x00-\x7F])+$/);

Negative lookahead is used to to make sure each ASCII character is not ".

Upvotes: 2

Related Questions