jen walters
jen walters

Reputation:

regular expression to disallow combination of characters

I am not very famliar with using anything but very basic regular expression. I have a field that allows all characters except single quote, double quote and question mark (I know, not a good idea, but what can I say. My customers will not budge on this requirement.) Now, a new requirement is added. The character combination of @# is also not allowed. My current regular expression is ^[^?'"]{0,1000}$ How do I now include the requirement of @# as a specific character combination that is not allowed?

Upvotes: 2

Views: 3811

Answers (3)

Luke Schafer
Luke Schafer

Reputation: 9265

Dave's has a problem - the requirement of the OP was to disallow the original characters and the specific combination of @#. Additionally, it's simpler to allow if the regular expression tests false in this case, rather than if it tests true, as the regex becomes much easier to read. It also negates the length requirement which can be done as a seperate test if required.

!val.test(/[^"'?]|@#/)

Upvotes: 0

VonC
VonC

Reputation: 1323115

Note: if lookahead, look-behind are supported, an alternative would be:

^(?:(?<!@)#|@(?!#)|[^'"?@#]){0,1000}$

Upvotes: 0

Dave
Dave

Reputation: 10577

Without the length limitation, you could do

^([^"'?@]|@+[^"#'?@])*@*$

Upvotes: 2

Related Questions