簡體   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