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

68
Views
Removing characters and changing color of text in label doesn't work

I want to replace every word starting with % with the same word without the % and changing its color to green.

However, all I can do is remove the % without changing colors.

Is there something I am missing here?

var text = "Lite %match Color"
text = text.replace(/%(.*?)/g, "<span style='color: green'>$1</span>")
document.getElementById("output").innerHTML = text
<label id="output"></label>

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

0

The problem with your regex is that, given that you have no lookahead after the .*?, it will match the least amount of characters, which is none at all. In order to fix this problem, you can substitute that part with \S+, that attempts to match any non-space characters (at least one).

var text = "Lite %match Color"
text = text.replace(/%(\S+)/g, "<span style='color: green'>$1</span>")
document.getElementById("output").innerHTML = text
<label id="output"></label>

about 4 years ago · Juan Pablo Isaza Report

0

var text = "Lite %match Color"
  text = text.replace(/\s+%(.*?)\s+/g, "<span style='color: green'> $1 </span>")
  document.getElementById("output").innerHTML = text
<div id="output"> </div>

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!