1

Saw a challenge on Twitter so I've been working my way through it, granted I am not the best with Regular Expressions. This is what I have so far:

var pass_regex = new RegExp(/^[a-z][A-Z][0-9]|[!@#$%^&*()_]+$/);

I am trying to match a password input that contains:

  • 1 Lowercase Letter
  • 1 Uppercase Letter
  • 1 Digit OR Special Character

Where I am getting stuck is on the 'OR' part, I thought the pipe separator between [0-9] and my set of special characters would work but it doesn't seem to. Trying to better understand how you would use regular expressions to to check for 1 Digit OR 1 Special Character. Thank you in advance for any help provided.

3
  • did you mean atleast one or exactly one. Commented Feb 3, 2015 at 1:47
  • The exact instruction from the challenge was "The password must contain a minimum of one lowercase character, one uppercase character, and one digit or special character." Commented Feb 3, 2015 at 1:51
  • 1
    for readability and maintainability, suggest to write the rules in separate units. passwd.match(/[a-z]/) && passwd.match(/[A-Z]/) && ( passwd.match(/0-9/) || passwd.match(/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/) ) Commented Feb 3, 2015 at 1:55

1 Answer 1

2

Atleast one:

You need to use a positive lookahead based regex for checking multiple conditions.

^(?=.*?[A-Z])(?=.*?[a-z]).*?[\W\d].*

OR

^(?=.*?[A-Z])(?=.*?[a-z]).*?[!@#$%^&*()_\d].*
  • (?=.*?[A-Z]) Asserts that there must be atleast one uppercase letter.
  • (?=.*?[a-z]) Atleast one lowercase letter.
  • .*? non-greedy match of any character zero or more times.
  • If the above conditions are satisfied then match that corresponding string and also the string must contain atleast a single character from the given list [!@#$%^&*()_\d] . \d in this list matches any digit character.
  • .* matches the following zero or more characters.

DEMO

Sign up to request clarification or add additional context in comments.

6 Comments

I appreciate your time helping me with this, however, as I am not very good with RegEx it is difficult for me to comprehend what you have provided above, if you have the time, could you please explain?
for condition checking purpose, .* at the last is unnecesary but op says I am trying to match a password input that contains, that is he's trying to match the whole password. So here .* is must :)
Thank you for your quick help. Here's a quick test I setup: var pass_regex = new RegExp(/^(?=.*?[A-Z])(?=.*?[a-z]).*?[!@#$%^&*()_\d].*/); if(!pass_regex.test(password.val())) { alert('Invalid Password'); validated = false; } But I am wondering, the password test Az1$ passes but I was trying to figure out an OR operator, meaning it should contain one digit OR one special character and this RegEx does not seem to handle that. Any ideas?
use var pass_regex = /^(?=.*?[A-Z])(?=.*?[a-z]).*?[!@#$%^&*()_\d].*/m; . Yep, Az1$ string satisfies the above condition of uppercase, atleast lowecase and a digit. So it got matched.
That seems to have done the trick, thank you. It appears that all that was changed was the addition of the 'm' character to the end, right? What exactly did that do to change the RegEx?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.