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

210
Views
How to surround a specific text with <span>

I'm trying to isolate a certain letter, v, in a case title by surrounding it with a <span> tag. This single character word has always space around it, yet I've tried to do it via word boundary match in regex: \b

(function() {
  let title = document.querySelector("h1>a");

  let a = title.innerHTML;

  let b = a.match(/\b[v]\b/i);

  console.log(b);

  title.innerHTML = a.replace(b, '<span>\1</span>');
})();
span {
  text-transform: lowercase;
  color: black;
  background-color: red;
}
<h1>
  <a href="http://example.com/"> Viva v Vanuatu</a>
</h1>

However, the above code won't work. It doesn't select an isolated v character and matches any v character it finds first.

I want it to be like this:

<h1>
  <a href="http://example.com/"> Viva <span>v</span> Vanuatu</a>
</h1>

No jquery please.

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

0

This?

%1 is the first captured group

(function() {
  let title = document.querySelector("h1>a");
  let a = title.textContent;
  title.innerHTML = a.replace(/\b(v)\b/, "<span>$1</span>");
})();
span {
  text-transform: lowercase;
  color: black;
  background-color: red;
}
<h1>
  <a href="http://example.com/"> Viva v Vanuatu</a>
</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!