如何使用JavaScript计算长字符串中多个子字符串的出现次数
我是JavaScript新手。我只是尝试了很多,但没有得到答案和信息来说明如何一次计算长字符串中多个子字符串的出现次数 进一步信息:我需要得到这些子字符串的出现次数,如果出现次数太多,我需要一次替换它们,所以我需要一次得到出现次数 以下是一个例子: 长字符串文本如下所示 超级碗50是一场美式足球比赛,旨在确定2015赛季美国国家足球联盟(NFL)的冠军。美国橄榄球大会(AFC)冠军丹佛野马队以24-10击败了美国橄榄球大会(NFC)冠军卡罗莱纳美洲豹队,赢得了他们的第三个超级碗冠军。比赛于2016年2月7日在加利福尼亚圣克拉拉旧金山湾旧金山湾体育场举行。由于这是第50届超级碗,联盟通过各种以黄金为主题的举措强调了“黄金纪念日”,并暂时中止了用罗马数字命名每一场超级碗比赛的传统(根据罗马数字,比赛将被称为“超级碗L”),因此,徽标可以突出显示阿拉伯数字50 子字符串是一个问题,但我需要的是一次计算该子字符串中出现的每个单词。例如,这个字符串中的单词“name”、“NFL”、“championship”、“game”和“is”、“the” NFL冠军赛的名字是什么 其中一个问题是文本中没有一些子字符串,有些子字符串已经显示了很多次。(我可能会替换它) 我试过的代码如下,它是错误的,我试过许多不同的方法,但没有好的结果如何使用JavaScript计算长字符串中多个子字符串的出现次数,javascript,string,Javascript,String,我是JavaScript新手。我只是尝试了很多,但没有得到答案和信息来说明如何一次计算长字符串中多个子字符串的出现次数 进一步信息:我需要得到这些子字符串的出现次数,如果出现次数太多,我需要一次替换它们,所以我需要一次得到出现次数 以下是一个例子: 长字符串文本如下所示 超级碗50是一场美式足球比赛,旨在确定2015赛季美国国家足球联盟(NFL)的冠军。美国橄榄球大会(AFC)冠军丹佛野马队以24-10击败了美国橄榄球大会(NFC)冠军卡罗莱纳美洲豹队,赢得了他们的第三个超级碗冠军。比赛于201
$(".showMoreFeatures").click(function(){
var text= $(".article p").text(); // This is to get the text.
var textCount = new Array();
// Because I use match, so for the first word "what", will return null, so
this is to avoid this null. and I was plan to get the count number, if it is
more than 7 or even more, I will replace them.
var qus = item2.question; //This is to get the sub-string
var checkQus = qus.split(" "); // I split the question to words
var newCheckQus = new Array();
// This is the array I was plan put the sub-string which count number less than 7, which I really needed words.
var count = new Array();
// Because it is a question as sub-string and have many words, so I wan plan to get their number and put them in a array.
for(var k =0; k < checkQus.length; k++){
textCount = text.match(checkQus[k],"g")
if(textCount == null){
continue;
}
for(var j =0; j<checkQus.length;j++){
count[j] = textCount.length;
}
//count++;
}
$(“.showMoreFeatures”)。单击(函数(){
var text=$(“.article p”).text();//这是为了获取文本。
var textCount=新数组();
//因为我使用match,所以对于第一个单词“what”,将返回null,所以
这是为了避免这个空值。我计划得到计数,如果是的话
超过7个甚至更多,我将替换它们。
var qus=item2.question;//这是为了获取子字符串
var checkQus=qus.split(“”;//我将问题拆分为单词
var newCheckQus=新数组();
//这是我计划放置的数组的子字符串,其计数小于7,这是我真正需要的单词。
var count=新数组();
//因为这是一个作为子字符串的问题,并且有很多单词,所以我计划获取它们的编号并将它们放入数组中。
对于(var k=0;k 对于(var j=0;j如果我正确理解了问题,那么您似乎需要计算问题中的单词(que
)在文本中出现的次数(txt
)
我将在香草JavaScript中介绍这一点,您可以根据需要将其转换为JQuery
首先,要关注文本,我们可以通过将字符串更改为小写并删除一些标点符号来简化事情
// both strings to lowercase
txt = txt.toLowerCase();
que = que.toLowerCase();
// remove punctuation
// using double \\ for proper regular expression syntax
var puncArray = ["\\,", "\\.", "\\(", "\\)", "\\!", "\\?"];
puncArray.forEach(function(P) {
// create a regular expresion from each punctuation 'P'
var rEx = new RegExp( P, "g");
// replace every 'P' with empty string (nothing)
txt = txt.replace(rEx, '');
que = que.replace(rEx, '');
});
现在我们可以从str
和que
创建一个更干净的数组,以及从que
创建一个哈希表,就像这样
// Arrays: split at every space
var txtArray = txt.split(" ");
var queArray = que.split(" ");
// Object, for storing 'que' counts
var queObject = {};
queArray.forEach(function(S) {
// create 'queObject' keys from 'queArray'
// and set value to zero (0)
queObject[S] = 0;
});
queObject
将用于保存已计数的单词。如果您要console.debug(queObject)
此时,它看起来像这样
console.debug(queObject);
/* =>
queObject = {
what: 0,
is: 0,
the: 0,
name: 0,
of: 0,
nfl: 0,
championship: 0,
game: 0
}
*/
// go through each element in 'queArray'
queArray.forEach(function(A) {
// create regular expression for testing
var rEx = new RegExp( A );
// test 'rEx' against elements in 'txtArray'
txtArray.forEach(function(B) {
// is 'A' in 'B'?
if (rEx.test(B)) {
// increase 'queObject' property 'A' by 1.
queObject[A]++;
}
});
});
现在我们要测试txtArray
中的每个元素,看看它是否包含queArray
中的任何元素。如果测试为true
,我们将向等效的queObject
属性添加+1,如下所示
console.debug(queObject);
/* =>
queObject = {
what: 0,
is: 0,
the: 0,
name: 0,
of: 0,
nfl: 0,
championship: 0,
game: 0
}
*/
// go through each element in 'queArray'
queArray.forEach(function(A) {
// create regular expression for testing
var rEx = new RegExp( A );
// test 'rEx' against elements in 'txtArray'
txtArray.forEach(function(B) {
// is 'A' in 'B'?
if (rEx.test(B)) {
// increase 'queObject' property 'A' by 1.
queObject[A]++;
}
});
});
我们在这里使用RegExptest
方法,而不是Stringmatch
方法,因为我们只想知道“B==true中的A是否为”。如果为true,则将相应的queObject
属性增加1。此方法还将查找单词内部的单词,例如“San Francisco”中的“is”等
如果一切正常,将queObject
记录到控制台将显示问题中的每个单词在文本中出现的次数
console.debug(queObject);
/* =>
queObject = {
what: 0
is: 2
the: 17
name: 0
of: 2
nfl: 1
championship: 0
game: 4
}
*/
希望有帮助。:)
有关以下方面的更多信息,请参阅MDN:
如果我正确理解了问题,那么您似乎需要计算问题中的单词(
que
)在文本中出现的次数(txt
)
我将在香草JavaScript中介绍这一点,您可以根据需要将其转换为JQuery
首先,要关注文本,我们可以通过将字符串更改为小写并删除一些标点符号来简化事情
// both strings to lowercase
txt = txt.toLowerCase();
que = que.toLowerCase();
// remove punctuation
// using double \\ for proper regular expression syntax
var puncArray = ["\\,", "\\.", "\\(", "\\)", "\\!", "\\?"];
puncArray.forEach(function(P) {
// create a regular expresion from each punctuation 'P'
var rEx = new RegExp( P, "g");
// replace every 'P' with empty string (nothing)
txt = txt.replace(rEx, '');
que = que.replace(rEx, '');
});
现在我们可以从str
和que
创建一个更干净的数组,以及从que
创建一个哈希表,就像这样
// Arrays: split at every space
var txtArray = txt.split(" ");
var queArray = que.split(" ");
// Object, for storing 'que' counts
var queObject = {};
queArray.forEach(function(S) {
// create 'queObject' keys from 'queArray'
// and set value to zero (0)
queObject[S] = 0;
});
queObject
将用于保存已计数的单词。如果您要console.debug(queObject)
此时,它看起来像这样
console.debug(queObject);
/* =>
queObject = {
what: 0,
is: 0,
the: 0,
name: 0,
of: 0,
nfl: 0,
championship: 0,
game: 0
}
*/
// go through each element in 'queArray'
queArray.forEach(function(A) {
// create regular expression for testing
var rEx = new RegExp( A );
// test 'rEx' against elements in 'txtArray'
txtArray.forEach(function(B) {
// is 'A' in 'B'?
if (rEx.test(B)) {
// increase 'queObject' property 'A' by 1.
queObject[A]++;
}
});
});
现在我们要测试txtArray
中的每个元素,看看它是否包含queArray
中的任何元素。如果测试为true
,我们将向等效的queObject
属性添加+1,如下所示
console.debug(queObject);
/* =>
queObject = {
what: 0,
is: 0,
the: 0,
name: 0,
of: 0,
nfl: 0,
championship: 0,
game: 0
}
*/
// go through each element in 'queArray'
queArray.forEach(function(A) {
// create regular expression for testing
var rEx = new RegExp( A );
// test 'rEx' against elements in 'txtArray'
txtArray.forEach(function(B) {
// is 'A' in 'B'?
if (rEx.test(B)) {
// increase 'queObject' property 'A' by 1.
queObject[A]++;
}
});
});
我们在这里使用RegExptest
方法,而不是Stringmatch
方法,因为我们只想知道“B==true中的A是否为”。如果为true,则将相应的queObject
属性增加1。此方法还将查找单词内部的单词,例如“San Francisco”中的“is”等
如果一切正常,将queObject
记录到控制台将显示问题中的每个单词在文本中出现的次数
console.debug(queObject);
/* =>
queObject = {
what: 0
is: 2
the: 17
name: 0
of: 2
nfl: 1
championship: 0
game: 4
}
*/
希望有帮助。:)
mor见MDN