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

187
Visualizações
regex to match all elements inside string

i want to get all strings between brackets []. i tried regex match methode and it's not working

i only get the first element changed

here is my code

tags = "[r,a,9]@[r,a,8].com".match(/([(?:[??[^[]*?]))/);
for (let index = 0; index < tagNumber; index++) {
  console.log(tags[index]);
  fromName = "[r,a,9]@[r,a,8].com".replace(tags[index], "haha");
}

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

0

/\[.+?\]/g

/ denotes the start of the pattern

\[ matches the [ character

.+? matches any character, 1 or more times, lazily

\] matches the ] character

/g denotes the end of the pattern and to find ALL matches

const input = "[r,a,9]@[r,a,8].com"
const pattern = /\[.+?\]/g

const tags = Array.from(input.match(pattern))
console.log(tags)

const fromName = input.replace(pattern, "haha")
console.log(fromName)

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to escape [ and ] to match them literally. There's no need for the capture group around everything.

Add the g modifier to match all the occurrences in the string.

tags = "[r,a,9]@[r,a,8].com".match(/\[[^[]*?\]/g);
let tagNumber = tags.length;
for (let index = 0; index < tagNumber; index++) {
  console.log(tags[index]);
  fromName = "[r,a,9]@[r,a,8].com".replace(tags[index], "haha");
  console.log(fromName)
}

You can use a regular expression in replace() to replace all the matches at once.

fromName = "[r,a,9]@[r,a,8].com".replace(/\[[^[]*?\]/g, "haha");
console.log(fromName)

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