How to insert a variable into regex in replace function with TypeScript

1 Answer

0 votes
let str = "CPP TypeScript CPP Python PHP CPP";
  
const word = "CPP";
const re = new RegExp(word, "gi");
   
str = str.replace(re, "C");
    
console.log(str);
  
  
 
 
                    
/*
run:
  
"C TypeScript C Python PHP C" 
   
*/

 

 



answered May 22, 2022 by avibootz
...