Как написать правильно регулярку чтобы каждый символ обернуть в span кроме br и удалить все пробелы

https://codepen.io/tigranmogrov/pen/jOMgJVJ Сам пытался написать но не вышло

let txt = document.querySelector('.app__title');

document.write(txt.innerHTML.replace(/(?![^<]*>)[^\s*/]/g, c => `<span>${c}</span>\n`));

Ответы (1 шт):

Автор решения: vsemozhebuty

Возможно, так?

const txt = document.querySelector('.app__title');

for (const node of [...txt.childNodes]) {
  if (node.nodeType !== Node.TEXT_NODE) continue;

  const text = node.textContent.replace(/\s/g, '');
  const fragment = new DocumentFragment();
  for (const character of text) {
    const span = document.createElement('span');
    span.textContent = character;
    fragment.appendChild(span);
  }
  txt.replaceChild(fragment, node);
}
.app__title{
  text-align:center;
}
        <h2 class="app__title">
                        時間が経つほど<br class="md">「選んでよかった」
                    </h2>

→ Ссылка