给定一个字符串,例如“wwwaaacvwwwwwwbnmddbbbbbbargg”,找出字符串中长度最长的连续字母。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| function getMoreValue(str) {
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));
|