Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda