是否有可能在JavaScript中连接2个正则表达式字符串

时间:2018-08-02 10:06:08

标签: javascript regex typescript ecmascript-6 ecmascript-5

我在javascript中有2个正则表达式字符串,我需要将它们合并为1个正则表达式。我看到可以使用|来完成此操作

示例:

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

这是怎么做的?

1 个答案:

答案 0 :(得分:0)

您可以这样做:

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