1

I need to match a string that has at least one number and one capital letter. I have this:

/[a-z][A-Z][0-9]+/ 

but this only matches a string with lowercase letters first, followed by uppercase letter and then a digit. I need to be able to match them in any order.

3 Answers 3

4

Just use a pair of look-ahead assertions:

(?=.*[A-Z])(?=.*\d).+ 

Regular expression visualization

Debuggex Demo

Edited to capture the string, not merely match it.

Note: you only need the final .+ if you want to capture the result. If you simply want to do a test for whether the string contains at least one number and one capital letter, you don't need that final .+. For example, this

'aB9'.match(/(?=.*[A-Z])(?=.*\d)/) 

returns [""], which evaluates as true if you put it in an if or while. On the other hand, this

'aB9'.match(/(?=.*[A-Z])(?=.*\d).+/) 

returns ["aB9"]. Use whichever flavor best suits your needs.

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

3 Comments

I like it and use it frequently. Wish I could claim credit for it. :)
You'll need to anchor it. Your demo isn't matching anything
@AmitJoki It works just fine in the sense that it matches the string; it just doesn't capture anything. That said, I edited my answer to capture the string, not merely match it. If yours was the downvote, I would appreciate if you removed it.
1

You need to anchor the lookaround if you want to capture it, otherwise it wont work.

But if you want to just test the string, Ed's solution works.

(?=.*[A-Z])(?=.*\d).* 

Regular expression visualization

Debuggex Demo

6 Comments

This answer needs clarification, as the regex works fine even without the final .*. For example, 'aB9'.match(/(?=.*[A-Z])(?=.*\d)/) returns a match; it just happens to be an empty string. It even works in your Debuggex demo without the final .*. That portion of the regex is only necessary to capture the string; it's not necessary if simply testing for a match.
@EdCottrell OP wants to match it
he says but this only matches a string with lowercase letters first, followed by uppercase letter and then a digit. I need to be able to match them in any order.
Nonetheless, it needs clarification. You wrote, "You need to anchor the lookaround, otherwise it wont work." That is true if you are looking to capture the text. It's incorrect if you merely want to match it, which is what OP asked about.
Looks good. By the way, if you were the downvote on my answer, I'd appreciate if you removed it, as I have also clarified my answer. :)
|
0

Regex Example

^.*(?=.*[A-Z]).*(?=.*\d).*$ 

Regular expression visualization

Debuggex Demo

Description:

This will match any searchtext with at least 1 capital letter and 1 digit.


Regex Example With forcing a minimum number of characters

\A(?=\S*?[A-Z])(?=\S*?[0-9])\S{6,}\z 

Regular expression visualization

Debuggex Demo

Regex Explained:

\A(?=\S*?[A-Z])(?=\S*?[0-9])\S{6,}\z \A assert position at start of the string (?=\S*?[A-Z]) Positive Lookahead - Assert that the regex below can be matched \S*? match any non-white space character [^\r\n\t\f ] Quantifier: Between zero and unlimited times, as few times as possible, expanding as needed [lazy] [A-Z] match a single character present in the list below A-Z a single character in the range between A and Z (case sensitive) (?=\S*?[0-9]) Positive Lookahead - Assert that the regex below can be matched \S*? match any non-white space character [^\r\n\t\f ] Quantifier: Between zero and unlimited times, as few times as possible, expanding as needed [lazy] [0-9] match a single character present in the list below 0-9 a single character in the range between 0 and 9 \S{6,} match any non-white space character [^\r\n\t\f ] Quantifier: Between 6 and unlimited times, as many times as possible, giving back as needed [greedy] \z assert position at the very end of the string 

Description:

The above regex requires 1 capital letter, 1 number, and at least 6 characters long

2 Comments

Why the \S*? and \S{6,}? OP never said anything about those requirements.
just incase he missed that...i'll add that as additional regex

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.