-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?

TKDev
  • 493
  • 1
  • 4
  • 19
  • Have you maybe tried if that's *how its done*? Wouldn't that answer your question? – Luca Kiebel Aug 02 '18 at 10:11
  • Can you not just run it yourself and see what happens? – Gareth Oates Aug 02 '18 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`. – phuzi Aug 02 '18 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? – Wiktor Stribiżew Aug 02 '18 at 11:42

1 Answers1

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  );