I need to come up with a regular expression that validates a string with the following requirements:
- Only contains alphanumeric and the following special characters: !@#$%^&*
- Contains at least 1 of the special characters in the list above
- The required special character can appear anywhere in the string (for example: !abc, a!bc, abc!)
I keep getting close, but fail on one of the conditions.
Thanks!
The regular expression you’re looking for is:
which will guarantee the string contains one of your special characters using (?=) positive lookahead.
–EDIT–
It appears Bohemian decided to completely ignore the comment section of my answer and edited the above example to remove its “unnecessary” character escaping. I really wish he hadn’t because I believe my justification is correct, but there you have it. My original example was: