Is it possible to use partial/substring match in the indexOf method of javascript ?
I have an String arraylist & i want to use partial text of the full string in the indexOf method to get the index number of the desired string in the 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")
,
instead of the above line i want to use something like:
var index=text.indexOf("belongs to any type of category")
I need to get index number 2 based on the text match hit i.e "Where does this %#@!$%&&*&(()(&$..: belongs to any type of category" but bcz of the special characters inside the string it is making it tuff & since its an array having other strings which i am getting dynamically is adding to the complications.
So i am trying to use the .indexOf method in which we can pass a string & it returns the index number, so my question here is there a way i could pass in a part of String instead of the entire string & get the index number 2 successfully ?
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