【问题标题】:How to find total count of consecutive repeated characters and numbers in a string using javascript?如何使用javascript查找字符串中连续重复字符和数字的总数?
【发布时间】:2022-12-16 15:48:45
【问题描述】:

我想获取字符串中连续重复字符和数字的总数。 例如 : const value = '11111aaaio222' 所以输出应该是11

解释:

“1”重复5次

“a”重复3次

“2”重​​复3次

【问题讨论】:

    标签: javascript


    【解决方案1】:

    const data = "11111aaaio222";
    
    const result = Array.from(new Set([...data])).reduce((p, c) => data.split(c).length > 3 ? p += data.split(c).length - 1 : p, 0)
    
    console.log(result);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 2019-09-02
      • 2019-12-13
      • 2011-11-01
      • 2022-11-02
      • 2020-01-16
      • 1970-01-01
      相关资源
      最近更新 更多