如何将三种正则表达式结合在一起?

时间:2016-06-17 18:03:18

标签: javascript regex

我想一起模式

1. Is
2. (https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})  -website regex pattern
3. down?

全部为一,我试过

/(Is(https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})\sdown?)/

但没有匹配。

我期望输出的是

  

codepen sample updated下降了吗? //返回true

2 个答案:

答案 0 :(得分:1)

var patterns = [
  /Is/,
  /(https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})/,
  /down\?/
];

var delimiter = /\s+/;  // or / / to match single spaces only

var re = new RegExp(patterns.map(pattern => pattern.source).join(delimiter.source));

console.log(re);
console.log(re.test('Is http://yahoo.com down?'));
console.log(re.test('Is http://socks.com down?'));

答案 1 :(得分:1)

您只需要部分之间的\s*,或\W+,如果还有一些标点符号:

/Is\s*(https?:\/\/(?:www\.|(?!www))[^\s.]+\.\S{2,}|www\.\S+\.\S{2,})\s*down\?/
   ^^^                                                              ^^^ 

请参阅regex demo

此外,[^\s] = \S

var s = "Is http://www.socks.com down?";
var re = /Is\s*(https?:\/\/(?:www\.|(?!www))[^\s.]+\.\S{2,}|www\.\S+\.\S{2,})\s*down\?/;
console.log(re.test(s));