fork download
  1. const SURROGATE_PAIR_RE = /(?:[\ud800-\udbff][\udc00-\udfff])/;
  2.  
  3. function countCodePoints (s) {
  4. const re = new RegExp(SURROGATE_PAIR_RE.source + "|[\\S\\s]", "g");
  5. let n = 0;
  6. while (re.exec(s))
  7. n++;
  8. return n;
  9. }
  10.  
  11. function odai_pt15_467 (s) {
  12. const re = new RegExp("(" + SURROGATE_PAIR_RE.source + "|[\\S\\s])\\1{1,}",
  13. "g");
  14. return s.replace(re, function (substring, character) {
  15. if (substring.length == 1)
  16. return substring;
  17. else
  18. return character + countCodePoints(substring);
  19. });
  20. }
  21.  
  22. for (let s of ["あいうえお",
  23. "ああいいうう",
  24. "あいうあいう",
  25. "ああああ",
  26. "🐤🐤🐤🐤"]) {
  27. print(odai_pt15_467(s));
  28. }
  29.  
Success #stdin #stdout 0.03s 17020KB
stdin
Standard input is empty
stdout
あいうえお
あ2い2う2
あいうあいう
あ4
🐤4