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?
+2
A:
Without the length limitation, you could do
^([^"'?@]|@+[^"#'?@])*@*$
Dave
2009-05-21 23:33:23
Thank you! This works great!
2009-05-21 23:58:09
A:
Note: if lookahead, look-behind are supported, an alternative would be:
^(?:(?<!@)#|@(?!#)|[^'"?@#]){0,1000}$
VonC
2009-05-22 00:04:51
A:
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(/[^"'?]|@#/)
Luke Schafer
2009-05-22 01:03:34