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

176
Vistas
Replace text if in parentheses and specific character before

Using JavaScript, I am trying to replace text if its in parentheses and the the parentheses has a specific character (A) before it.

A string can look like this:

A(This text) should (say) hello.

Here A(This text) should be replaced with You, but (say) should not be replaced at all, because it does not have the character A before it. Also the character A has to be in uppercase.

I have tried with regex but I find it very hard to understand. Right now I can only remove A and then replace whats inside parentheses, but this also removes other text inside parentheses that does not have A before them.

This is what I got:

const string = "A(This text) should (say) hello."

function runIt() {
 const replace = string.replace(/ *\A() */g, "You") //Removes "A"
 let replace2 = replace.replace(/ *\([^)]*\) */g, " ") //"Removes all text inside all parentheses

 console.log(replace2) //Result is: "You should hello.", it should say: "You should (say) hello.
}

runIt();
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Use

\bA\([^()]+\)

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  A                        'A'
--------------------------------------------------------------------------------
  \(                       '('
--------------------------------------------------------------------------------
  [^()]+                   any character except: '(', ')' (1 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  \)                       ')'

JavaScript code

const string = "A(This text) should (say) hello."

function runIt(string) {
  let replace2 = string.replace(/\bA\([^()]+\)/g, 'You')
  console.log(replace2)
}

runIt(string)

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