如何计算特定字符串在另一个字符串中出现的次数。例如,这就是我试图在Javascript中做的事情:

var temp = "This is a string.";
alert(temp.count("is")); //should output '2'

当前回答

此函数在三种模式下工作:查找字符串中单个字符的频率,查找字符串中相邻子字符串的频率,然后如果它与一个匹配,则会直接向前移动到它后面的下一个,第三个与前一个相似,但它也会计算给定字符串中的交叉子字符串

函数substringFrequency(字符串、子字符串、连接){let索引允许发生频率=0for(设i=0;i<string.length;i++){index=string.indexOf(substring,i)如果(索引!=-1){if((子字符串长度==1)||连接==true){i=索引}其他{i=索引+1}发生频率++}其他{打破} }return(发生频率)}console.log(substringFrequency('vvv','v'))console.log(substringFrequency('vvv','vv'))console.log(substringFrequency('vvv','vv'))

其他回答

正则表达式(global的缩写)中的g表示搜索整个字符串,而不仅仅是查找第一个出现的字符串。此匹配是两次:

var temp=“这是一个字符串。”;var count=(temp.match(/is/g)| |[]).length;console.log(计数);

如果没有匹配项,则返回0:

var temp=“Hello World!”;var count=(temp.match(/is/g)| |[]).length;console.log(计数);

/** Function that count occurrences of a substring in a string;
 * @param {String} string               The string
 * @param {String} subString            The sub string to search for
 * @param {Boolean} [allowOverlapping]  Optional. (Default:false)
 *
 * @author Vitim.us https://gist.github.com/victornpb/7736865
 * @see Unit Test https://jsfiddle.net/Victornpb/5axuh96u/
 * @see https://stackoverflow.com/a/7924240/938822
 */
function occurrences(string, subString, allowOverlapping) {

    string += "";
    subString += "";
    if (subString.length <= 0) return (string.length + 1);

    var n = 0,
        pos = 0,
        step = allowOverlapping ? 1 : subString.length;

    while (true) {
        pos = string.indexOf(subString, pos);
        if (pos >= 0) {
            ++n;
            pos += step;
        } else break;
    }
    return n;
}

用法

occurrences("foofoofoo", "bar"); //0

occurrences("foofoofoo", "foo"); //3

occurrences("foofoofoo", "foofoo"); //1

允许重叠

occurrences("foofoofoo", "foofoo", true); //2

比赛:

  foofoofoo
1 `----´
2    `----´

单元测试

https://jsfiddle.net/Victornpb/5axuh96u/

基准

我做了一个基准测试,我的功能超过了10倍比gumbo发布的regexp匹配函数更快。在我的测试中字符串长度为25个字符。字符“o”出现2次。我在Safari中执行了1000 000次。Safari 5.1基准>执行总时间:5617ms(正则表达式)基准测试>执行总时间:881毫秒(我的功能快6.4倍)Firefox 4基准>执行总时间:8547毫秒(Rexep)基准测试>总执行时间:634毫秒(我的功能更快13.5倍)编辑:我所做的更改缓存的子字符串长度为字符串添加了类型转换。添加了可选的“allowOverlapping”参数修复了“”空子字符串大小写的正确输出。

Gist

https://gist.github.com/victornpb/7736865

基于@Vittim.us的上述回答。我喜欢他的方法给我的控制,使其易于扩展,但我需要添加不区分大小写的功能,并将匹配限制在支持标点符号的整个单词中。(例如,“洗澡”是指“洗澡”,而不是“洗澡”)

标点正则表达式来自:https://stackoverflow.com/a/25575009/497745(如何使用正则表达式从JavaScript字符串中删除所有标点符号?)

function keywordOccurrences(string, subString, allowOverlapping, caseInsensitive, wholeWord)
{

    string += "";
    subString += "";
    if (subString.length <= 0) return (string.length + 1); //deal with empty strings

    if(caseInsensitive)
    {            
        string = string.toLowerCase();
        subString = subString.toLowerCase();
    }

    var n = 0,
        pos = 0,
        step = allowOverlapping ? 1 : subString.length,
        stringLength = string.length,
        subStringLength = subString.length;

    while (true)
    {
        pos = string.indexOf(subString, pos);
        if (pos >= 0)
        {
            var matchPos = pos;
            pos += step; //slide forward the position pointer no matter what

            if(wholeWord) //only whole word matches are desired
            {
                if(matchPos > 0) //if the string is not at the very beginning we need to check if the previous character is whitespace
                {                        
                    if(!/[\s\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&\(\)*+,\-.\/:;<=>?@\[\]^_`{|}~]/.test(string[matchPos - 1])) //ignore punctuation
                    {
                        continue; //then this is not a match
                    }
                }

                var matchEnd = matchPos + subStringLength;
                if(matchEnd < stringLength - 1)
                {                        
                    if (!/[\s\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&\(\)*+,\-.\/:;<=>?@\[\]^_`{|}~]/.test(string[matchEnd])) //ignore punctuation
                    {
                        continue; //then this is not a match
                    }
                }
            }

            ++n;                
        } else break;
    }
    return n;
}

如果发现错误或改进,请随时修改和重构此答案。


subst_count从php转换为Javascript


Locutus(将Php翻译为JS的包)subst_count(官方页面,代码复制如下)

function substr_count (haystack, needle, offset, length) { 
  // eslint-disable-line camelcase
  //  discuss at: https://locutus.io/php/substr_count/
  // original by: Kevin van Zonneveld (https://kvz.io)
  // bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
  // improved by: Brett Zamir (https://brett-zamir.me)
  // improved by: Thomas
  //   example 1: substr_count('Kevin van Zonneveld', 'e')
  //   returns 1: 3
  //   example 2: substr_count('Kevin van Zonneveld', 'K', 1)
  //   returns 2: 0
  //   example 3: substr_count('Kevin van Zonneveld', 'Z', 0, 10)
  //   returns 3: false

  var cnt = 0

  haystack += ''
  needle += ''
  if (isNaN(offset)) {
    offset = 0
  }
  if (isNaN(length)) {
    length = 0
  }
  if (needle.length === 0) {
    return false
  }
  offset--

  while ((offset = haystack.indexOf(needle, offset + 1)) !== -1) {
    if (length > 0 && (offset + needle.length) > length) {
      return false
    }
    cnt++
  }

  return cnt
}

查看Locutus对Php的subst_count函数的翻译

试试看:

function countString(str, search){
    var count=0;
    var index=str.indexOf(search);
    while(index!=-1){
        count++;
        index=str.indexOf(search,index+1);
    }
    return count;
}