检查字符串是否包含忽略大小写(JavaScript)的数组中的元素



我有这个代码来获得需要根据检查的字符串

var f = document.getElementsByClassName('first');
var c = f[0];
var xx = c.getElementsByTagName('a')[0];
var chk = xx.innerText;

和阵列

const ar = ["[Text]", "[More]", "[AnotherText]", "[Stuff]", "[Yes]"]

有问题的字符串(chk(可以包含该数组中的任何值,并且可以是任何情况下的

示例:[TExT] some random text afterwards

我需要检查chk是否包含ar中的任何值,忽略情况

尝试

if (ar.some(chk => chk.toLowerCase().includes(chk))){console.log("yay")}

if (ar.some(chk.includes.bind(chk)))

但它们返回未定义的

的更改

chk => chk.toLowerCase().includes(chk)

val => val.toLowerCase().includes(chk.toLowerCase())

const ar = ["[Text]", "[More]", "[AnotherText]", "[Stuff]", "[Yes]"];
const isContain = (arr, chk) =>
arr.some(
(val) =>
val.toLowerCase().includes(chk.toLowerCase()) ||
chk.toLowerCase().includes(val.toLowerCase())
);


console.log(isContain(ar, "tExt"))

最新更新