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

100
Views
How to properly escape the characters in this regex construction

I am building a naive rstrip function. And it works properly like this when I hardcode the actual characters:

const rstrip = (s, chars) => s.replace(/[r!]+$/g, "");
console.log(rstrip("Hello!!!!r", '!r'))

However, if I try and use the chars variable, I have trouble escaping it:

const rstrip = (s, chars) => s.replace(/[chars]+$/g, "");
console.log(rstrip("Hello!!!!r", '!r'))

What would be the proper way to 'escape' the variable so that the value !r replaces the chars variable in the replace method?

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

0

const rstrip = (s, chars) => s.replace(new RegExp(`[${chars}]+$`, 'g'), "");
console.log(rstrip("Hello!!!!r", '!r'))

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!