How to use variable in RegExp with JavaScript

1 Answer

0 votes
let str = "javascript php go python php phpphphp php java";
const word = "php";
const replacewith = "C";
 
str = str.replace(new RegExp(word, "gi"), replacewith);
 
console.log(str);
 
 
    
/*
run:
    
javascript C go python C CChp C java
 
*/

 



answered Aug 24, 2024 by avibootz
edited Aug 25, 2024 by avibootz
...