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

147
Visualizações
Regex replace strings matching pattern with multiple charercters

I have a string like this

const str = "Today is going to be @[City Temp](city-temp) in @[City Name](city-name)"

I want the output to be in handlebars syntax eg

const str = "Today is going to be {{city-temp}} in {{city-name}}"

I am having trouble trying to match this pattern.

I can see that if I do

str.replace(/[()]/g,"")

I get the string without the brackets.

But I can't work out how to search for the whole pattern something like the below where I find anything starting with @[ and ending with ), I don't want to just start with @ by itself as there may be times in the string I don't want to remove the @ where it is not followed by a [

/[\@\[A-Za-z0-9\](A-Za-z0-9-)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use

str = str.replace(/@\[[^\][]*]\(([^()]*)\)/g, "{{$1}}")

See the regex demo. Details:

  • @\[ - a @[ string
  • [^\][]* - zero or more chars other than [ and ]
  • ]\( - a ]( string
  • ([^()]*) - Group 1 ($1): any zero or more chars other than ( and )
  • \) - a ) char.

See the JavaScript demo:

const str = "Today is going to be @[City Temp](city-temp) in @[City Name](city-name)"
console.log(str.replace(/@\[[^\][]*]\(([^()]*)\)/g, "{{$1}}"))

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