Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

305
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!