首页 文章

检查数组值是否包含html中的特定值

提问于
浏览
3

我有一个像下面的angularjs数组:

var test=["abc/1/1","def/2/2","efg/3/3"];

我想检查一个数组是否包含html中包含“abc”的值

我检查了:

test.indexOf("abc")!=-1

但它的回归 false

我在JS中填充了数组,我想检查数组是否包含html中包含abc的值 .

button form =“myAttributeAdd”type =“submit”data-ng-disabled =“”class =“btn btn-success btn-xs”> Save

这是我的html中的按钮元素 . 这里data-ng-disabled将为true或false,具体取决于数组数据 . 如果数组数据值包含abc,则将启用其他禁用

1 回答

  • 1

    您可以使用RegExp在其中搜索abc .

    //getting html value
    var valueToFind = document.getElementById('htmlCode').innerText;
    
    function findValue(findString) {
      //getting arrays to be find
      var test = ["abc/1/1", "def/2/2", "efg/3/3"];
      //creating the regex
      var reg = new RegExp(findString);
      
      //a.match will show the output
      console.log(findString,test.some(a => a.match(reg)))
    
      console.log(findString,test.filter(a => a.match(reg)))
    
    }
    
    //invoking the function
    findValue('abc');
    findValue(valueToFind);
    
    <div id="htmlCode">efg</div>
    

相关问题