基于正则表达式过滤数组

时间:2018-03-17 12:55:35

标签: javascript arrays regex

我有以下数组:

array = ['S2A_MSIL1C_20180213T041901_N0206_R090_T46QCH_20180213T075744.SAFE','S2B_MSIL1C_20180208T041929_N0206_R090_T46QCJ_20180208T075342.SAFE','S2A_MSIL1C_20180213T041901_N0206_R090_T46QCJ_20180213T075744.SAFE','S2B_MSIL1C_20180310T041559_N0206_R090_T46QDK_20180310T075716.SAFE']

我使用此正则表达式过滤此数组:

regex = new RegExp('(T46QCJ|s)','g');

并将其应用于数组

var filter = array.filter(e => regex.test(e));

输出

["S2B_MSIL1C_20180208T041929_N0206_R090_T46QCJ_20180208T075342.SAFE"]

而不是期望的结果

['S2B_MSIL1C_20180208T041929_N0206_R090_T46QCJ_20180208T075342.SAFE','S2A_MSIL1C_20180213T041901_N0206_R090_T46QCJ_20180213T075744.SAFE']

为什么没有插入第二场比赛?

0 个答案:

没有答案