我在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 );
这是怎么做的?
答案 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 );