Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

pass variable to regrex literal notation javascript

// If you want to get ALL occurrences (g), be case insensitive (i), and use boundaries so that it isn't a word within another word ():

re = new RegExp(`${replaceThis}`, 'gi');

// example:

let inputString = "I'm John, or johnny, but I prefer john.";
let replaceThis = "John";
let re = new RegExp(`${replaceThis}`, 'gi');
console.log(inputString.replace(re, "Jack")); // I'm Jack, or johnny, but I prefer Jack.
Comment

PREVIOUS NEXT
Code Example
Javascript :: access to xmlhttprequest has been blocked by cors policy react 
Javascript :: convert date to timestamp javascript 
Javascript :: disable a button react 
Javascript :: ajax with progress bar 
Javascript :: regex is not empty string 
Javascript :: mongo updatemany query 
Javascript :: javascript interval fixed number of times 
Javascript :: dynamic event listener jquery 
Javascript :: javascript get element by id 
Javascript :: unstringify json js 
Javascript :: useeffect clearinterval loading 
Javascript :: syntax for srcset in react 
Javascript :: link stylesheet in javascript 
Javascript :: javascript break out of loop 
Javascript :: check if input is valid js 
Javascript :: ERESOLVE unable to resolve dependency tree npm ERR npm ERR! Found: @angular/core@5.0.3 npm ERR! node_modules/@angular/core 
Javascript :: array map limit javascript 
Javascript :: set span text jquery 
Javascript :: change image src onclick javascript 
Javascript :: javascript round to 2 decimals 
Javascript :: nodejs reverse string 
Javascript :: js get last element of array 
Javascript :: distance to km javascript 
Javascript :: foreach javascript arrow function 
Javascript :: jquery hover and hover out 
Javascript :: how to hide react navigation header in react native 
Javascript :: How to make blinking/flashing text with jQuery 
Javascript :: javascript canvas beziercurveto 
Javascript :: how hide .html in url 
Javascript :: Attach token with http request angular 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =