How to use variable in RegExp with TypeScript

1 Answer

0 votes
let str: string = "phptypescript php c++ python php phphp";
const word = "php";
const replacewith = "GO";
 
str = str.replace(new RegExp(word, "gi"), replacewith);
 
console.log(str);
 
 
    
/*
run:
    
"GOtypescript GO c++ python GO GOhp" 
 
*/

 



answered Aug 25, 2024 by avibootz

Related questions

1 answer 121 views
1 answer 97 views
1 answer 119 views
1 answer 180 views
1 answer 130 views
...