How to use variable in RegExp with Node.js

1 Answer

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

 



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

Related questions

1 answer 140 views
1 answer 100 views
1 answer 97 views
1 answer 96 views
1 answer 128 views
2 answers 177 views
...