You need to double escape any RegExp characters (once for the slash in the string and once for the regexp):

"$TESTONE $TESTONE".replace(new RegExp("\\$TESTONE","gm"), "foo")

Otherwise, it looks for the end of the line and 'TESTONE' (which it never finds).

Personally, I'm not a big fan of building regexp's using strings for this reason. The level of escaping that's needed could lead you to drink. I'm sure others feel differently though and like drinking when writing regexes.

Answer from seth on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replace
String.prototype.replace() - JavaScript | MDN
The whole string being examined. ... An object whose keys are the used group names, and whose values are the matched portions (undefined if not matched). Only present if the pattern contains at least one named capturing group. The exact number of arguments depends on whether the first argument is a RegExp object — and, if so, how many capture groups it has. The following example will set newString to 'abc - 12345 - #$*%': ... function replacer(match, p1, p2, p3, offset, string) { // p1 is non-digits, p2 digits, and p3 non-alphanumerics return [p1, p2, p3].join(" - "); } const newString = "abc12345#$*%".replace(/(\D*)(\d*)(\W*)/, replacer); console.log(newString); // abc - 12345 - #$*%
Discussions

How Replace a string in regex
Hello Community, I want to replace a word (loves) with another word (hates) by using regex, such that i just replace the text content and not the values in the Tags. Input: My dog loves dog food My dog loves dog food Output: My dog **hates** dog food My dog **hates** dog food More on forum.uipath.com
🌐 forum.uipath.com
6
0
June 16, 2023
Regex advice - replace reoccurring strings in javascript
Hi all, Does anyone know how to use regex with javascript to replace a reoccurring pattern in a string? e.g. KEEPTHISDATA I have tried all kinds of replace patterns but I can't get it to work. The above example is just a simple cut down version of the string but the idea is there. More on community.retool.com
🌐 community.retool.com
1
0
August 11, 2023
How do I incorporate   in regular expressions ?
You can stick it in a string with hex escape codes. 160 would be '\xa0', if I can do math this late at night. See if your regex engine works if you just stick some of those in the literal. More on reddit.com
🌐 r/Python
13
10
June 18, 2014
How do you replace a character in a string with a single backslash?

r/learnpython is probably a better subreddit for these kinds of questions.

Having said that, your first example actually works, try:

print("apple".replace('l', '\\'))
More on reddit.com
🌐 r/Python
7
0
March 16, 2015
🌐
W3Schools
w3schools.com › jsref › jsref_replace.asp
JavaScript String replace() Method
RegExp Reference · The replaceAll() Method - replaces all matches · string.replace(searchValue, newValue) A global, case-insensitive replacement: let text = "Mr Blue has a blue house and a blue car"; let result = text.replace(/blue/gi, "red"); Try it Yourself » ·
🌐
freeCodeCamp
freecodecamp.org › news › javascript-string-replace-example-with-regex
JavaScript String.Replace() Example with RegEx
October 20, 2020 - To use RegEx, the first argument of replace will be replaced with regex syntax, for example /regex/. This syntax serves as a pattern where any parts of the string that match it will be replaced with the new substring.
🌐
Mimo
mimo.org › glossary › javascript › replace
JavaScript Replace Method: Advanced String Manipulation
For more advanced patterns, you can use regular expressions (regex). ... let text = "Hello world!"; let newText = text.replace("world", "Mimo"); console.log(newText); // Outputs: "Hello Mimo!" You can also use string.prototype.replace, which is the built-in method directly tied to JavaScript strings for efficient string manipulation.
🌐
Bennadel
bennadel.com › blog › 2198-special-references-in-javascripts-string-replace-method.htm
Special $ References In JavaScript's String.replace() Method
April 21, 2020 - Ben Nadel explores the $&, $`, and $' as special regular expression references available in Javascript's String.replace() method.
Find elsewhere
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Regular_expressions
Regular expressions - JavaScript | MDN
This allows you to do new RegExp(RegExp.escape("a*b")) to create a regular expression that matches only the string "a*b".
🌐
DEV Community
dev.to › maafaishal › javascript-stringreplace-useful-cases-3963
JavaScript `string.replace()` useful cases - DEV Community
September 24, 2024 - let str = "Hello World, World!"; let result = str.replace(/world/gi, "JavaScript") // Output: "Hello JavaScript, JavaScript!"
🌐
Codemzy
codemzy.com › blog › regex-groups-with-replace
Regex groups with .replace() in Javascript - Codemzy's Blog
February 2, 2022 - Regex groups are super useful when you need to replace specific patterns in strings. In this blog post, discover how to use multiple groups to perform simple (and complex) replacements.
🌐
Bennadel
bennadel.com › blog › 142-ask-ben-javascript-string-replace-method.htm
Ask Ben: Javascript String Replace Method
April 18, 2020 - Ask Ben: Getting Query String Values In JavaScript · Ask Ben: Cleaning Two Digit Years Using Javascript And Regular Expressions ... Excellent summary, thanks. Aug 23, 2006 at 12:28 AM ... There are a LOT of scripts out there that use replace() assuming it'll do a global replace without using a regex (and yes, they're all broken).
🌐
CoreUI
coreui.io › blog › how-to-replace-all-occurrences-of-a-string-in-javascript
How to replace all occurrences of a string in JavaScript? · CoreUI
August 31, 2024 - Replacing all occurrences of a string in JavaScript can be accomplished using several methods: Regular expressions with the global flag (g) provide flexibility and power. split and join offer a non-regex alternative. replaceAll is the most modern ...
🌐
Medium
medium.com › @jfagbohungbe › using-the-string-replace-method-in-javascript-d03cee4d843e
Using the string.replace method in Javascript | by Oluwajuwon Fagbohungbe | Medium
October 18, 2019 - What we want to do is find all ... matching, we can create an expression that matches what we want to replace. const regExp = /[^a-z][a-z]/gi ·...
🌐
UiPath Community
forum.uipath.com › help › community
How Replace a string in regex - Community - UiPath Community Forum
June 16, 2023 - Hello Community, I want to replace a word (loves) with another word (hates) by using regex, such that i just replace the text content and not the values in the Tags. Input: My dog loves dog food My dog loves dog food Output: My dog **hates** dog food My dog **hates** dog food
🌐
Krasimirtsonev
krasimirtsonev.com › blog › article › Javascript-replace-and-regular-expression
Javascript string replace and regular expression
June 15, 2013 - That's, of course, possible and ... method: var changeText = function(word) { var r = new RegExp("(" + word + ")", "gi"); // global match and ignore case flag element.innerHTML = text.replace(r, '[$1](#)'); } changeText('git');...
🌐
Dmitri Pavlutin
dmitripavlutin.com › replace-all-string-occurrences-javascript
3 Ways To Replace All String Occurrences in JavaScript
January 27, 2023 - The string method string.replace(regExpSearch, replaceWith) searches and replaces the occurrences of the regular expression regExpSearch with replaceWith string.
🌐
Retool
community.retool.com › 💬 app building
Regex advice - replace reoccurring strings in javascript - 💬 App Building - Retool Forum
August 11, 2023 - Hi all, Does anyone know how to use regex with javascript to replace a reoccurring pattern in a string? e.g. <5.01>KEEPTHISDATA</5.67><5.100>KEEPTHISDATATOO></5.500> I have tried all kinds of replace patterns but I c…
🌐
RegExr
regexr.com
RegExr: Learn, Build, & Test RegEx
RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp). Supports JavaScript & PHP/PCRE RegEx.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-program-replace-specific-words-with-another-word-in-a-string-using-regular-expressions
Replace Specific Words in a String Using Regular Expressions in JavaScript - GeeksforGeeks
July 23, 2025 - In this approach, we are Using String.replace() with a regular expression allows you to find and replace specific words or patterns within a string efficiently, enabling global replacements for all occurrences of the target.
🌐
Sentry
sentry.io › sentry answers › javascript › how do i replace all occurrences of a string in javascript?
JavaScript replaceAll: Replace All String Occurrences | Sentry
The replaceAll() method has two arguments: pattern and replacement. It returns a new string with all matches of the pattern replaced by a replacement. The pattern is a string or regular expression (RegExp).