gpt4 book ai didi

javascript - 如何测试数组项的文本是否包含子字符串?

转载 作者:行者123 更新时间:2023-11-30 13:01:29 26 4
gpt4 key购买 nike

我一直在研究测试数组中是否存在一个值,并不断获取 indexOf('textvalue') 的匹配项。

但是,我的“文本值”将是一个子字符串,因此不会进行匹配。这是因为我将字段名称传递给方法,只需要删除“Door: Front Left;”通过匹配“门”上的数组项来输入文本。有谁知道我该怎么做?

这是我正在处理的示例代码:

(spnSelectedDisclosures 将有一个选定字段的列表,这是从文本中删除先前字段选择的代码,以分号分隔。)

var currentText = $("#spnSelectedDisclosures").text();

var existingArr = currentText.split(";")

for (i=0;i < existingArr.length;i++) {

var indexItem = " " + existingArr[i].toString(); // why is this still an object, instead of a string? Adding a space to it was a desperate try to make indexItem a string variable.

if (indexItem.contains(substringText)) { // offending line, saying the object has no method 'contains'
alert("there's been a match at: " + i);
textToRemoveIndex = i;
break;
}
}

var textToRemove = existingArr[textToRemoveIndex];
var newText = currentText.replace(textToRemove,"");
$("#spnSelectedDisclosures").text(newText);

最佳答案

这是怎么回事?

for (i=0;i < existingArr.length;i++) {
if (existingArr[i].indexOf(substringText) > -1) {
alert("there's been a match at: " + i);
textToRemoveIndex = i;
break;
}
}

关于javascript - 如何测试数组项的文本是否包含子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17303318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com