【进阶】找出字符串中长度最长的连续字母

Posted by ARTROY on 2019-10-30

给定一个字符串,例如“wwwaaacvwwwwwwbnmddbbbbbbargg”,找出字符串中长度最长的连续字母。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function getMoreValue(str) {
/**
* 正则: 反向引用
* ["www", "aaa", "c", "v", "wwwwww", "b", "n", "m", "dd", "bbbbbb", "a", "r", "gg"]
*/
const arr = str.match(/(\w)\1*/g);
const maxLength = Math.max(...arr.map(i => i.length));
const result = arr.reduce((pre, current) => {
if (current.length === maxLength) {
pre[current[0]] = current.length;
}
return pre;
}, {});
return result;
}

const t = 'wwwaaacvwwwwwwbnmddbbbbbbargg';
console.log(getMoreValue(t)); // {w: 6, b: 6}


支付宝打赏 微信打赏

欣赏此文,打赏一下



-->