I need a JavaScript regular expression that allows all alphanumeric characters and rejects "&" (ampersand), ";" (semicolon), and " ‘ " (apostrophe).
I know how to allow only certain characters, such as ^[a-zA-Z0-9 @$]*$—this allows all alphanumeric characters, spaces, "@" (at-sign), and "$" (dollar-sign).
How could I tell it to allow all special characters except "&", ";" and " ‘ ", while allowing all alphanumeric ones?
If the whole purpose is to allow everything except for the
&,;, and'symbols, you could just ignore those:This should allow all alpha-numeric symbols and every other special character.
If your list of acceptable/rejected characters is more complicated, it may be easier to write a regex specifying what’s allowed, like your original sample of
^[a-zA-Z0-9 @$]*$. It all depends on what your needs are and/or if they’ll change (I, personally, would prefer your original regex if it’s not going to change much).