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.
Related Posts:
- Javascript split regex question
- What is a good regular expression to match a URL? [duplicate]
- What is a good regular expression to match a URL? [duplicate]
- Is there a difference between /\s/g and /\s+/g?
- RegEx Ignore Case
- Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
- Escaping Discord subset of markdown
- How to test valid UUID/GUID?
- How can I remove a character from a string using JavaScript?
- Regular expression : match either of two conditions?
- RegExp in TypeScript
- Converting any string into camel case
- I want to remove double quotes from a String
- Regular expression for removing whitespaces
- Javascript Regular Expression Remove Spaces
- How can I extract a number from a string in JavaScript?
- Remove not alphanumeric characters from string
- Extract hostname name from string
- Difference between ?:, ?! and ?=
- Remove all special characters with RegExp
- How do you use a variable in a regular expression?
- How to count string occurrence in string?
- Regular Expression to reformat a US phone number in Javascript
- Jquery Value match Regex
- RegEx: How can I match all numbers greater than 49?
- window.location.replace() not working to redirect browser
- Regular expression to validate US phone numbers?
- javascript includes() case insensitive
- RegEx for Javascript to allow only alphanumeric
- Replace spaces with dashes and make all letters lower-case
- Check user logged in with JS
- “Uncaught SyntaxError: Cannot use import statement outside a module” when importing ECMAScript 6
- How to replace all occurrences of a string in JavaScript
- How to play audio?
- How do I test for an empty JavaScript object?
- Tallest Unicode character?
- How to solve ‘Redirect has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header’?
- jQuery setTimeout() Function [duplicate]
- In reactJS, how to copy text to clipboard?
- SyntaxError: Adjacent JSX elements must be wrapped in an enclosing tag
- Uncaught SyntaxError: Invalid or unexpected token
- JavaScript TypeError: Cannot read property ‘style’ of null
- Make javascript alert Yes/No Instead of Ok/Cancel
- How to create a dialog with “yes” and “no” options
- Adding images to a Bootstrap Carousel with AJAX
- XMLHttpRequest cannot load XXX No ‘Access-Control-Allow-Origin’ header
- Uncaught TypeError: Cannot read property ‘props’ of null
- How to use iMonkey in an iOS app
- ES6: Create Strings using Template Literals – Freecodecamp
- Youtube api – stop video
- Uncaught TypeError: Cannot read property ‘msie’ of undefined – jQuery tools
- Angular JS Uncaught Error: [$injector:modulerr]
- Nested objects in javascript, best practices
- How do I convert an integer to binary in JavaScript?
- What does window.jQuery and window.$ mean?
- How to change date format in JavaScript
- OnChange event handler for radio button (INPUT type=”radio”) doesn’t work as one value
- InvalidValueError: not an instance of HTMLInputElement
- How to clear react-native cache?
- How to set Angular 4 background image?
- React-Router : What is the purpose of IndexRoute?
- How to implement “select all” check box in HTML?
- JQuery get the title of a button
- How to open a URL using PHP?
- Trying to load jquery into tampermonkey script
- Atom JavaScript Autocomplete
- How to format a UTC date as a `YYYY-MM-DD hh:mm:ss` string using NodeJS?
- JavaScript button onclick not working
- Invariant Violation: _registerComponent(…): Target container is not a DOM element
- Wildcard string comparison in Javascript
- jQuery select option elements by value
- How to change cursor from pointer to finger using jQuery?
- How to get value in an object’s key using a variable referencing that key?
- Button that refreshes the page on click
- jQuery equivalent of body onLoad
- How to make `setInterval` behave more in sync, or how to use `setTimeout` instead?
- Adding Google Translate to a web site
- How do I include js without register/enqueue?
- How to remove p / br elements from gutenbergs editor
- contact form 7 repeatable fields calculations [closed]
- Listening change event of taxonomy term checkboxes in Gutenberg editor
- Accessing theme customizer farbtastic object instances
- Customizer JS API: Defining active_callback for a section
- Don’t allow JavaScript in the content area
- How to locate a Javascript? It is there in HTML but not in any file [closed]
- Malware gdjfgjfgj235f [closed]
- CDATA removing new line in script tag in wordpress
- How to run Javascript popup modal in a loop?
- Console Messages: A cookie associated with a cross-site resource at
- Proper way of minifiying java script files in wordpress theme
- Enqueue concatinated JS file in WordPress
- None of the JavaScript works when using wp_editor
- Why can’t I load JS script in a plugin?
- Block pattern conflict with custom block
- Masonry grid with bootstrap 4
- How to detect if we are in the Site Editor part of the Block Editor (as opposed to editing a Page/Post) in JavaScript?
- How can I show a Slot/Fill in the block editor
- How to add more elements to an already existing section in Gutenberg
- Get Comment Text via REST API
- Sending GravityForms to custom HTML