How to replace all occurrences of a word in a string with TypeScript

3 Answers

0 votes
let s = "typescript php c++ PHP css";
    
s = s.replace(/ php/g, ' python');
 
console.log(s);
  
   
   
    
    
/*
run:
    
"typescript python c++ PHP css" 
   
*/

 



answered Jan 23, 2022 by avibootz
0 votes
let s = "typescript php c++ PHP css";
    
s = s.replace(/ php/gi, ' python');
 
console.log(s);
  
   
   
    
    
/*
run:
    
"typescript python c++ python css" 
   
*/

 



answered Jan 23, 2022 by avibootz
0 votes
let s = "typescript php c++ PHP css";
    
s = s.split('php').join('python');
 
console.log(s);
  
   
   
    
    
/*
run:
    
"typescript python c++ PHP css" 
   
*/

 



answered Jan 23, 2022 by avibootz
...