是否可以在javascript的indexOf方法中使用partial / substring匹配?
我有一个String arraylist&我想在indexOf方法中使用完整字符串的部分文本来获取arraylist中所需字符串的索引号。
var text[]=
0----This text over here $%*&*&*&(()(&$..: matches
1----%#@!$%*&*&*&(()(&$..: text to be matched
2----Where does this %#@!$%*&*&*&(()(&$..: belongs to any type of category
3----There are many %#@!$%*&*&*&(()(&$..: comparators
var index=text.indexOf("Where does this %#@!$%*&*&*&(()(&$..: belongs to any type of category")
,
而不是上面的行,我想使用类似的东西:
var index=text.indexOf("belongs to any type of category")
我需要根据文本匹配命中得到索引号2,即“这个%#@!$%& & *&(()(& $ .. :属于任何类型的类别“ 但是字符串里面的特殊字符的bcz正在使它成为凝灰岩和因为它的一个数组有其他字符串,我正在动态地增加了复杂性。
所以我试图使用.indexOf方法,我们可以在其中传递一个字符串&它返回索引号,所以我的问题是有一种方法可以传入String的一部分而不是整个字符串&成功获得索引号2?
Code Tried:
describe('angularjs homepage todo list', function() {
var index = 'not found';
var text1 = "";
it('test', function() {
var textToFind = "belongs to any type of category";
for (var i=0;i<10;i++){
var results_list=element.all(By.xpath("//*[@id='panel']/div/div[2]/span[1]")).get(i).getText().then(function(text) {
text1=text1+"\n"+text;
console.log("promise returned text inside function is "+text1);
return text1;
})
}
console.log('Text via global defined variable text1 is ' + text1);
getIndex(0, text1.length, text1, textToFind);
console.log('index is ' + index);
});
function getIndex(i, max, array, textToFind) {
if (i < max) {
console.log('text[' + i + '].indexOf = ' + array[i].indexOf(textToFind))
if (array[i].indexOf(textToFind) > 0) {
index = i;
//The index number will be assigned to the variable index
//if indexOf is greater than 0, e.g. 38 was returned on index 2
} else {
getIndex(i + 1, max, array, textToFind);
}
}
}
});
Started
Started execution of test
Text via global defined variable text1 is
index is not found
Test Case passed
promise returned text inside function is ['This text over here $%*&*&*&(()(&$..: matches',
'%#@!$%*&*&*&(()(&$..: text to be matched',
'Where does this %#@!$%*&*&*&(()(&$..: belongs to any type of category',
'There are many %#@!$%*&*&*&(()(&$..: comparators']
1 spec, 0 failures
Finished in 58.462 seconds
[18:35:47] I/launcher - 0 instance(s) of WebDriver still running
[18:35:47] I/launcher - internet explorerANY #01 passed
答案 0 :(得分:1)
我需要完全更新答案:
describe('testing', function() {
var index = 'not found';
var text1 = [];
it('should push element to array', function() {
browser.ignoreSynchronization = true;
browser.get('https://www.w3schools.com/angular/');
var elm = element(by.id('leftmenuinner')).all(By.css('[target="_top"]'));
elm.count().then(function(count) {
pushToArray(0, count, elm);
})
});
it('should identify index', function() {
var textToFind = "Data Binding";
getIndex(0, text1.length, text1, textToFind);
console.log('Text via global defined variable text1 is ' + text1);
console.log('index is ' + index);
});
function getIndex(i, max, array, textToFind) {
if (i < max) {
console.log('text[' + i + '].indexOf = ' + array[i].indexOf(textToFind))
if (array[i].indexOf(textToFind) > 0) {
index = i;
//The index number will be assigned to the variable index
//if indexOf is greater than 0, e.g. 38 was returned on index 2
} else {
getIndex(i + 1, max, array, textToFind);
}
}
}
function pushToArray(i, max, elm) {
if (i < max) {
elm.get(i).getText().then(function(tmpText) {
console.log(tmpText);
text1.push(tmpText);
})
pushToArray(i + 1, max, elm);
}
}
});
我希望我使用的示例网站与您正在尝试的内容完全吻合 如果这样可行,您仍然可以更新它以缩短代码。