Search code examples
javascriptregexregex-negation

RegEx sample help for Javascript


I am looking to build a regex for JavaScript.

  • Condition 1 Range a-z,A-Z,0-9 are mandatory,
  • Condition 2 can have any of these special chars. ~!@#$&*(),;:'-_

Other chars should fail.

Edit

I did try /[a-zA-Z0-9][\~\!\@\#\$*0\-_]/ It got failed for ^


Solution

  • Use a regexp that matches all the allowed characters, along with a lookahead that matches at least one of the required characters. The regexp needs to be anchored to match the entire input.

    (?=.*[a-zA-Z0-9])^[a-zA-Z0-9~!@#$&*(),;:'\-_]*$
    

    DEMO