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在这里没有帮助。