繁体   English   中英

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

[英]Check if string contains an element from array ignoring case (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"))

暂无
暂无

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

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