Syntax for several positive results in javascript regex

I am trying to include two positive expressions in one regex. Here is the problem I'm working on as an example.

(?=[a-zA-Z])(?=[0-9])[a-zA-Z0-9]{0,20}

This is what I am trying to match:

  • 0-20 characters
  • one or more letters anywhere
  • one or more numbers anywhere
  • only letters and numbers are allowed.

When I do this with only one look, it works, but as soon as I add another, it breaks. What is the correct syntax for two views?

+5
source share
2 answers

Lookaheads, ! -, , . .* ( lazy .*?), .

@AlexR, RegEx:

^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9_]{0,20}$

, , .

:

^(?=[^a-zA-Z]*[a-zA-Z])(?=\D*\d)\w{1,20}$
+9

@revos , : 01234567890123456789A . ^ $, ( , ):

^(?=.{0,19}[a-zA-Z])(?=.{0,19}[0-9])[a-zA-Z0-9]{0,20}$ // (1), (1*) without ^
^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]{0,20}$
(?=.{0,19}[a-zA-Z])(?=.{0,19}[0-9])[a-zA-Z0-9]{0,20} // (2)

. ^ , ..

Input            : "Password1 = ASDF0123"
Matches with (1) : none
Matches with (1*): "ASDF0123"
Matches with (2) : "Password1", "ASDF0123"
+1

All Articles