I want to restrict usage of unescaped ampersands in a particular input field. I\'m having trouble getting a RegEx to kill usage of \"&\" unless followed by \"amp;\"...or per
You can match on /&(?!amp;)/
to locate any &'s not followed by &. The (?!) construction is called a negative lookahead.
Assuming you're using a regexp engine that supports them, at any rate. I know Perl/PCRE regexps do.
This regular expression matches any occurrence of &
which is not followed by amp;
:
/&(?!amp;)/
Rubular
This regular expression accepts strings that contain characters except &
, or the string &
:
/^([^&]|&)*$/
Rubular
You can use either one or the other, depending on which is most convenient. The difference is that the string should be rejected if the first regular expression matches, whereas the string should be accepted if the second regular expression matches.