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

179
Views
Javascript - How to remove texts in between brackets

I'd like to know a simple way to change:

My name[1] is David Smith[ref.2] and my home is in Auburn[Geo-Ref3], AL.

into

My name is David Smith and my home is Auburn, AL.

If I do

string.replace(/\[.*\]/g, "")

then I get

My name, AL.

Thanks.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

.* will match greedily (as much as possible) by default. So the regex matched

My name[1] is David Smith[ref.2] and my home is in Auburn[Geo-Ref3], AL.
       ^----------------------------------------------------------^

You want to match reluctantly (as little as possible), which has syntax .*?. So use string.replace(/\[.*?\]/g, ""). This will match

My name[1] is David Smith[ref.2] and my home is in Auburn[Geo-Ref3], AL.
       ^-^               ^-----^                         ^--------^
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!