是否可以在javascript的indexOf方法中使用部分/子字符串匹配?

我有一个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成功?

尝试过的代码:

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

最佳答案

我需要完全更新答案:

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);
        }
    }
});


我希望我使用的示例网站与您尝试的内容完全匹配。
如果可行,您仍然可以对其进行更新以使代码更短。

07-24 09:50
查看更多