-1

I have 2 regex strings in javascript and I need to concat them into 1 regex. I saw somewhere this could be done using |

example:

passwordRegex:RegExp = '(?=.*[A-Za-z])(?=.*\\d)(?=.*[$@$!%*#?&^])[A-Za-z\\d$@$!%*#?&^]{8,}';
hasFourConsecutiveRegex:RegExp  = '(.)\\1\\1\\1';
combinedRegex:RegExp = new RegExp(this.passwordRegex.source  + ' | ' + this.hasFourConsecutiveRegex.source );

is this how its done?

4
  • Have you maybe tried if that's how its done? Wouldn't that answer your question? Commented Aug 2, 2018 at 10:11
  • Can you not just run it yourself and see what happens? Commented Aug 2, 2018 at 10:11
  • It depends what your reasons are for concatenating the expressions. What you have done is essentially create an or. Either it matches passwordRegex or (|) it matches hasFourConsecutiveRegex. Commented Aug 2, 2018 at 10:13
  • What are you trying to do? Validate an 8+ char string that contains at least one ASCII letter, one digit, one special char from the defined set and that has 4 identical consecutive chars? Commented Aug 2, 2018 at 11:42

1 Answer 1

0

You can do like this:

var passwordRegex = new RegExp('(?=.*[A-Za-z])(?=.*\\d)(?=.*[$@$!%*#?&^])[A-Za-z\\d$@$!%*#?&^]{8,}');
var hasFourConsecutiveRegex = new RegExp('(.)\\1\\1\\1'); 
var combinedRegex = new RegExp(passwordRegex.source   + ' | ' + hasFourConsecutiveRegex.source  );
Sign up to request clarification or add additional context in comments.

1 Comment

@TKDev, please accept this answer if it work for you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.