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

306
Vistas
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 Respuestas
Responde la pregunta

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 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