JavaScript函数自动计算字符串中的连续字母 [英] JavaScript function to automatically count consecutive letters in a string
问题描述
我正在尝试(未成功),以编写JavaScript函数 LetterCount 来计算字符串中连续的字母(而不是总数).
理想情况下: LetterCount("eeeeeoooohhoooee")= [["e",5],["o",3],["h",2],["o",3],["e",2]] >
下面的代码仅在我已经知道它们是什么时才尝试计算字符串中的连续字母数:
function LetterCount(str) {
for (var i=0; i<str.length;i++) {
var arr1=[]; arr2=[]; arr3=[]; arr4=[]; arr5=[];
var e=0; o=0; h=0; o2=0; e2=0;
if(str[i]="e") {
arr1 += "e";
e++;
}
arr1.push(e);
if(str[i]="o") {
arr2 += "o";
o++;
}
arr2.push(o);
if(str[i]="h") {
arr3 += "h";
h++;
}
arr3.push(h);
if(str[i]="o") {
arr4 += "o";
o2++;
}
arr4.push(o2);
if(str[i]="e") {
arr5 += "e";
e2++;
}
arr5.push(e2);
}
return arr1.concat(arr2).concat(arr3).concat(arr4).concat(arr5);
}
在上面的代码中,我首先需要知道字符串中的字母是什么,以及以什么顺序出现了多少个字母.
INSTEAD:如何编写一个函数,该函数将自动识别字母本身,然后返回连续字母的计数.如果答案采用以下格式,那也将很棒:
LetterCount("eeeeeoooohhoooee") = [["e", 5],["o",3],["h",2],["o",3],["e",2]]
非常感谢您的帮助!
您可以使用正则表达式匹配任意字母,然后匹配零个或多个相同字母的实例.
rx =/([[a-zA-Z])\ 1 */g;
您的示例匹配["eeeee","oooo","hh","ooo","ee"].
使用地图,为每个索引返回新数组中的首字母和出现次数.
function letterCount(str){
var s= str.match(/([a-zA-Z])\1*/g)||[];
return s.map(function(itm){
return [itm.charAt(0), itm.length];
});
}
letterCount("eeeeeoooohhoooee")
返回值:(数组)
[[["e",5],["o",4],["h",2],["o",3],["e",2]]
注意:
- var s = str.match(/([[a-zA-Z]] \ 1 */g)|| [];
返回一个匹配项数组(重复的字母)或一个空数组([]).否则,如果字符串不包含任何字母,则将引发错误(从在null上调用map).
-
\ 1 * 用于允许匹配单个字母的实例,且不重复或不重复. '\ 1+'与单个未重复的字母不匹配.
-
数组 map 需要一个函数并传递三个参数-每个索引处的值,索引号以及对整个数组的引用.在这种情况下,仅使用每个索引的值,因此我们可以忽略其他参数.
I am attempting (unsuccessfully) to write JavaScript function LetterCount to count the consecutive letters in a string (and not the total number).
Ideally: LetterCount("eeeeeoooohhoooee") = [["e", 5],["o",3],["h",2],["o",3],["e",2]]
The following code attempts to count the number of consecutive letters in a string only when I already know what they are:
function LetterCount(str) {
for (var i=0; i<str.length;i++) {
var arr1=[]; arr2=[]; arr3=[]; arr4=[]; arr5=[];
var e=0; o=0; h=0; o2=0; e2=0;
if(str[i]="e") {
arr1 += "e";
e++;
}
arr1.push(e);
if(str[i]="o") {
arr2 += "o";
o++;
}
arr2.push(o);
if(str[i]="h") {
arr3 += "h";
h++;
}
arr3.push(h);
if(str[i]="o") {
arr4 += "o";
o2++;
}
arr4.push(o2);
if(str[i]="e") {
arr5 += "e";
e2++;
}
arr5.push(e2);
}
return arr1.concat(arr2).concat(arr3).concat(arr4).concat(arr5);
}
In the code above, I need to first know what the letters in the string are, and how many of them are present, in what order.
INSTEAD: How do you write a function that will automatically recognize the letter themselves, and then return the count of consecutive letters. Would also be great if the answer is in the following format:
LetterCount("eeeeeoooohhoooee") = [["e", 5],["o",3],["h",2],["o",3],["e",2]]
Any help is much appreciated!
You can use a regular expression to match any letter followed by zero or more instances of the same letter.
rx=/([a-zA-Z])\1*/g;
Your example matches ["eeeee","oooo","hh","ooo","ee"].
Using map, return the initial letter and the number of occurences in a new array for each index.
function letterCount(str){
var s= str.match(/([a-zA-Z])\1*/g)||[];
return s.map(function(itm){
return [itm.charAt(0), itm.length];
});
}
letterCount("eeeeeoooohhoooee")
returned value: (Array)
[["e",5],["o",4],["h",2],["o",3],["e",2]]
NOTES:
- var s= str.match(/([a-zA-Z])\1*/g)||[];
returns an array of matches (repeated letters) or an empty array([]). Otherwise, if the string does not contain any letters an error would be thrown (from calling map on null).
\1* is used to allow matching instances of a single letter with any or no sequential repetition. '\1+' would not match a single unrepeated letter.
Array map expects a function and passes three arguments- the value at each index, the index number, and a reference to the entire array. In this case, only the value of each index is used, so we can ignore the other arguments.
这篇关于JavaScript函数自动计算字符串中的连续字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!