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

307
Visualizações
remove square brackets and it content from string in javascript

How to remove characters between two brackets and brackets. For example

Let it [Am]be, let it [C/G]be, let it [F]be, let it [C]be
[C]Whisper words of [G]wisdom, let it [F]be [C/E] [Dm] [C]

I want above string to

Let it be, let it be, let it be, let it be
Whisper words of wisdom, let it be  

I use this regx but it is only take out brackets.

var reg = /[\[\]']+/g
var x = song.replace(reg,"")
console.log(x);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to escape the outer [] so you're matching those literally, and allow anything except a ] inside. So:

var reg = /\[[^\]]*\]/g;

Here's a breakdown (also on regex101):

  • \[ matches [ literally
  • [^\]] matches any character that isn't a ]
  • * says "zero or more" of those (so it replaces []; if you don't want that, change this to + for "one or more")
  • \] matches ] literally (I probably don't need to escape it there, but I tend to anyway)

Live Example:

const song = `Let it [Am]be, let it [C/G]be, let it [F]be, let it [C]be
[C]Whisper words of [G]wisdom, let it [F]be [C/E] [Dm] [C]`;

var reg = /\[[^\]]*\]/g;
var x = song.replace(reg,"");
console.log(x);


Side note: In modern JavaScript, we don't use var. Use let or const instead.

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