Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:在textarea中查找关键字的数量_Javascript_Jquery - Fatal编程技术网

Javascript:在textarea中查找关键字的数量

Javascript:在textarea中查找关键字的数量,javascript,jquery,Javascript,Jquery,输入: var text = $('#id_textarea').val(); var keywords = $('#id_keywords').val().split(','); text = "jquery javascript js jquery-js js django python, blah blah blah..."; keywords = ['jquery', 'js']; // so result = 3 输出:文本中的关键字数 示例: var text = $('#id_

输入:

var text = $('#id_textarea').val();
var keywords = $('#id_keywords').val().split(',');
text = "jquery javascript js jquery-js js django python, blah blah blah...";
keywords = ['jquery', 'js'];
// so result = 3
输出:文本中的
关键字数

示例:

var text = $('#id_textarea').val();
var keywords = $('#id_keywords').val().split(',');
text = "jquery javascript js jquery-js js django python, blah blah blah...";
keywords = ['jquery', 'js'];
// so result = 3
谢谢你的帮助:)


更新:我需要一个函数将结果作为输出。谢谢

未经测试,但逻辑应该可以工作

var textParts = text.split(' ');
var count = 0;

for (var index = 0; index < keywords.length; index++) {
    for (var i = 0; i < textParts.length; i++) {
        if (textParts[i].match(keywords[index])) {
            count++; // found match, increment count
        }
    }
}

alert(count); // alert the amount of matches found

这与其说是一个问题,不如说是一个要求我们为他做这件事的请求。你看过jQuery的函数列表了吗?这个问题不是jQuery特有的,而是关于如何获得字符串中特定关键字的匹配数的一般性问题。jQuery在这里没有帮助。