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

87
Views
Remove any of multiple characters at beginning and end of string

I want to remove the following characters: [, ], ", \ if any are at the beginning or end of a string, but not if they are in the middle.

So, if the string is ""\[\]][hello" world "\[\"]], I'd like it to become, hello" world

I can't figure out how to do it. I've tried these and many others and none seems to work:

string.replace(/^[\[+|\]+|\"+|\\+]$,"")
string.replace(/^[\[+\]+\"+\\+]$/,"")

Any help would be much appreciated.

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

0

You're close. You need two alternatives, one for the beginning and another for the end. Each should then be a character set in [] listing all the characters you want to remove.

You also need the g modifier to replace twice in the same string.

let string = '""\[\]][hello" world "\[\"]]';

console.log(string.replace(/^["\[\]\\]+|["\[\]\\]+$/g, ''));

about 4 years ago · Juan Pablo Isaza Report

0

I split the problem into following subproblems:

  1. Find these characters at the start of a string
  2. Find these characters at the end of a string

I came up with following regex-patterns to solve these:

  1. /(^["\[]]+)/g
  2. /["\[]]+$/g

When working with regex, I recommend following site: regexr.com/6d193

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!