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

107
Views
Javascript Regex Replace Without Resolving Backreference

I'm trying to replace a password token [**PASSWORD**] in a string with the password and am having trouble with passwords that contain the string $&.

For example:

var re = new RegExp('(?:\\[\\*\\*PASSWORD\\*\\*\\])', 'g');
'Your temporary password is: [**PASSWORD**]'.replace(re, 'B9z3$&dd');

I want the output to be Your temporary password is: B9z3$&dd, but instead the $& is resolving the backreference to [**PASSWORD**] which makes the output Your temporary password is: B9z3[**PASSWORD**]dd

How can I prevent Javascript from resolving the backreference and simply insert the text as-is?

Thank you!

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

0

You need to escape the $ in your replacement string doubling it: B9z3$$&dd.

Here's a demo:

result = 'Your temporary password is: [**PASSWORD**]'.replace(/\[\*\*PASSWORD\*\*\]/mg, "B9z3$$&dd");

console.log(result);

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!