Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

78
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda