【问题标题】:Get an array of matches in string as indexes获取字符串中的匹配数组作为索引
【发布时间】:2016-04-26 16:34:30
【问题描述】:

示例:"abc abc ab a".indexOfList("abc") 返回 [0,4]

我的代码:

String.prototype.indexOfList=function(word){
    var l=[];
    for(var i=0;i<this.length;i++){ //For each character
        var pushed=(this[i]==word[0])&&(word.length+i<=this.length);
        if (pushed) {
            for(var j=1;j<word.length;j++){
                if (word[j]!=this[i+j]) {
                    pushed=false; break;
                }
            }
        }
        if (pushed) {
            l.push(i);
        }
    }
    return l;
}

还有比这更好更小的方法吗?

【问题讨论】:

    标签: javascript string arraylist string-matching


    【解决方案1】:

    您可以使用正则表达式match 命令:

    var re = /abc/g,
    str = "abc abc ab a";
    
    var pos = [];
    while ((match = re.exec(str)) != null) {
        pos.push(match.index);
    }
    

    【讨论】:

      【解决方案2】:

      有一个版本可以处理重叠字符串,即模式 aaa 用于字符串 aaaaa 应该返回 [0,1,2]

      function indexOfList(needle, haystack) {
        const result = [];
        let i = 0;
        while (haystack.includes(needle, i)) {
          const match = haystack.indexOf(needle, i);
          result.push(match);
          i = match + 1;
        }
        return result;
      }
      
      indexOfList("abc", "abc abc ab a"), // [0, 4]
      indexOfList("aaa", "aaaabc abc ab a") // [0, 1]
      

      我还建议不要扩展原生对象的原型。这可能会导致非常讨厌的名称冲突。

      考虑你的同事(甚至是语言维护者)添加一个同名的函数。

      【讨论】:

      • 我正在考虑这种方法,但用子字符串切断每个找到的单词。
      【解决方案3】:

      带有indexOf功能

      var str = "abc abc ab a";
      var i = -1;
      var result = [];
      
      while (true) {
          i = str.indexOf("abc", i + 1);
          if (i == -1) break;
          result.push(i);
      }
      
      document.write(result);

      【讨论】:

        【解决方案4】:

        无需使事情复杂化。与此非常相似的方法已经存在:String.indexOf

        您还可以向此方法传递第二个参数,告诉它从哪里开始查找。如果你不断增加这第二个参数,你可以快速找到每一个出现的地方。

        String.prototype.indexOfList = function(word) {
            var start = this.indexOf(word);
            var l = [start]
        
            if(start == -1) {
                return [-1, -1];
            }
        
            var index = start;
            for(var i = start + word.length; i < this.length - word.length; i = index) {
                index = this.indexOf(word, i);
        
                if(index == -1) {
                    break;
                }
        
                l.push(index);
            }
        
            return l;
        }
        

        这将从第一次出现开始,并继续在单词出现的每个索引上添加。

        【讨论】:

        • 您的方法返回开始和结束索引。他想要字符串中每个匹配项的开始索引。
        • @HugoS.Mendes 我明白了。我误解了这个问题,现在编辑。
        【解决方案5】:

        你可以使用replace

        String.prototype.indexOfList = function(word){
            var l=[];
            this.replace(new RegExp(word,"g"), (a,i) => l.push(i));
            return l;
        }
        
        console.log("abc abc ab a".indexOfList("abc"));

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-04-08
          • 1970-01-01
          • 2017-07-28
          • 1970-01-01
          • 2014-12-07
          • 2014-08-21
          • 2013-11-22
          相关资源
          最近更新 更多