简体   繁体   English

array.includes使用正则表达式返回false

[英]array.includes returns false using regex

I have this array: 我有这个数组:

array = ['S2B_MSIL1C_20180310T041559_N0206_R090_T46QEK_20180310T075716.SAFE'];

and this regex: 和这个正则表达式:

regex = new RegExp('S2B_MSIL1C_20180310T041559_N0206_R090_T46QEK_20180310T075716' + '.SAFE','g');

When I use array.includes(regex); 当我使用array.includes(regex); , false is returned. ,则返回false Have I missed something? 我错过了什么吗?

Use Array.some 使用Array.some

var yourRegex = /pattern/g ;
var atLeastOneMatches = array.some(e => yourRegex.test(e));

Array.some returns true after the first one in the array returns true. 在数组中第一个返回true之后,Array.some返回true。 If it goes through the whole array with no true , it returns false. 如果它遍历整个数组而不为true ,则返回false。

RgExps are not for searching on Arrays, and includes method is for finding if your required object is included on the array or not. RgExps不适用于在数组上搜索,而includes方法用于查找所需的对象是否包含在数组中。 and here you passed and Regex object to your include method so it tells you that there is no regex object included your array. 在这里,您将Regex对象传递给了include方法,因此它告诉您数组中不包含regex对象。

you have to do one of the belows: 您必须执行以下一项操作:

array.includes('S2B_MSIL1C_20180310T041559_N0206_R090_T46QEK_20180310T075716' + '.SAFE');

or 要么

var yourRegex = /pattern/g ;
for(var i = 0 ; i<arr.length ; i++)
{
    if(yourRegex.test(arr[i]))
    {
        //Founded
        return true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM