With a regular expression, you can capture a character and then backreference that same character 1 or more times. Use a replacer function to replace the matched section with the character and the number of characters in the full match:
function countLetter(str) {
return str
.replace(/s/g, '')
.replace(/(w)1+/g, (match, char) => char + match.length);
}
console.log(countLetter('lorrem ipsssum dollorrrrum'));
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…