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

180
Vistas
Regex is unable to find and replace a string containing parentheses

I am attempting to find a sentence in a paragraph (contained in a textarea), and highlight it. All works well if the sentence does NOT contain opening and closing parenthesis, such as the example below. The replace method is unable to replace the string with its equivalent surrounded by the "mark" tags. Can anyone advise how I can update my Regex to find and replace a string containing parentheses? Do I need to find and replace all parentheses with "/(" and "/)" first?

const text = "An economy is composed of suppliers (workers) and demanders (firms)."

const regex = new RegExp(text, "\d");
// regex returns: /The market for labor, then, is composed of suppliers (workers) and demanders (firms). /
        console.log("REGEX:", regex);
        let match = text.replace(regex, "<mark>$&</mark>");
        console.log("MATCH:", match);
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Highlight Text in Parenthesis

html:

<html>
  <body>
   <p id="txt">"(An economy is composed of suppliers (workers) and demanders (firms).)</p>
  </body>
</html>

JS:

function hilitetext() {
  const s = document.getElementById("txt").textContent;
  const regex = /(?:^[^(]?[^(]+)?(\(\w+\))/g;//retains parenthesis
  //const regex = /(?:^[^(]?[^(]+)?\((\w+)\)/g;//removes parenthesis
  document.getElementById("txt").textContent = s.replace(regex, "<mark>$1</mark>");
}
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