Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
Split sentence into letters and add dynamic class (position of that letter) to it

I 'am trying to split a sentence/word into separate letters and was able to achieve using below code.

var textWrapper = document.getElementById("heading");
    textWrapper.innerHTML = textWrapper.textContent.replace(/\S/g, "<span class='letter'>$&</span>");
<h1 class="heading" id="heading">
 Hello!
</h1>

Input

<h1> Hello! </h1>

What I get from this

<h1>
 <span class="letter">H</span>
 <span class="letter">e</span>
 <span class="letter">l</span>
 <span class="letter">l</span>
 <span class="letter">o</span>
 <span class="letter">!</span>
</h1>

What I want

<h1>
  <span class="letter-1">H</span>
  <span class="letter-2">e</span>
  <span class="letter-3">l</span>
  <span class="letter-4">l</span>
  <span class="letter-5">o</span>
  <span class="letter-6">!</span>
</h1>

What can I do to achieve this?

textWrapper.innerHTML = textWrapper.textContent.replace(/\S/g, "<span class='letter-{what should I do here?}'>$&</span>");

If it is not possible this way, I am ok with some other way of achieving this.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I have zero knowledge in regex so heres how i would do it

    const heading = document.getElementById("heading");
    const letters = heading.innerText.split("").map((letter, index) => {
      const span = document.createElement("span");
      span.innerText = letter;
      span.classList.add("letter-" + (index + 1));
      return span;
    });
    heading.innerHTML = "";
    letters.forEach((span) => heading.appendChild(span));
about 4 years ago · Juan Pablo Isaza Report

0

You have to use some kind of loop to add the character index also

var textWrapper = document.getElementById( "heading" );
// `<span class='letter-${++index}'>$&</span>`
const text = textWrapper.textContent.trim();
textWrapper.innerHTML = "";

text.split("").forEach((char, i) => {
  const span = document.createElement("span");
  span.classList.add(`letter-${i + 1}`);
  span.textContent = char;
  textWrapper.appendChild(span);
})
  <h1 class="heading" id="heading">
    Hello!
  </h1>

about 4 years ago · Juan Pablo Isaza Report

0

If you really want to use regex for this you can pass a callback to replace() and access the match and offset of the match within the entire string.

var textWrapper = document.getElementById('heading');
textWrapper.innerHTML = textWrapper.textContent.replace(
  /\S/g,
  (match, offset) => `<span class='letter_${offset}'>${match}</span>`
);

console.log(textWrapper.outerHTML);
<h1 class="heading" id="heading">
 Hello!
</h1>

If you don't want to rely on the offset but instead want a controlled counter you can increment your own, here using a closure

((c)=>(match) => `<span class='letter_${c++}'>${match}</span>`)(1)

var textWrapper = document.getElementById('heading');
textWrapper.innerHTML = textWrapper.textContent.replace(
  /\S/g,
  ((c) => (match) => `<span class='letter_${c++}'>${match}</span>`)(1)
);

console.log(textWrapper.outerHTML);
<h1 class="heading" id="heading">
 Hello!
</h1>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!