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

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

当前回答

Leandro Batista的答案:只是正则表达式有问题。

“使用严格”;var dataFromDB=“testal”;$('input[name=“tbInput”]').on(“change”,function(){var charToTest=$(this).val();var howManyChars=charToTest.length;var nrMatches=0;如果(howManyChars!==0){charToTest=charToTest.charAt(0);var regexp=新regexp(charToTest,'gi');var arrMatches=dataFromDB.match(正则表达式);nrMatches=arrMatches?arrMatches.length:0;}$('#result').html(nrMatches.toString());});<script src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js“></script><div class=“main”>你想数什么<input type=“text”name=“tbInput”value=“”><br/>出现次数=<span id=“result”>0</span></div>

其他回答

/** 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

你可以试试这个

let count = s.length - s.replace(/is/g, "").length;

Leandro Batista的答案:只是正则表达式有问题。

“使用严格”;var dataFromDB=“testal”;$('input[name=“tbInput”]').on(“change”,function(){var charToTest=$(this).val();var howManyChars=charToTest.length;var nrMatches=0;如果(howManyChars!==0){charToTest=charToTest.charAt(0);var regexp=新regexp(charToTest,'gi');var arrMatches=dataFromDB.match(正则表达式);nrMatches=arrMatches?arrMatches.length:0;}$('#result').html(nrMatches.toString());});<script src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js“></script><div class=“main”>你想数什么<input type=“text”name=“tbInput”value=“”><br/>出现次数=<span id=“result”>0</span></div>

函数countInstance(字符串,单词){返回字符串.split(word).length-1;}console.log(countInstance(“This is a string”,“is”))

试试看

<?php 
$str = "33,33,56,89,56,56";
echo substr_count($str, '56');
?>

<script type="text/javascript">
var temp = "33,33,56,89,56,56";
var count = temp.match(/56/g);  
alert(count.length);
</script>