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

3 Answers

0 votes
let s = "javascript php c++ PHP css";
   
s = s.replace(/ php/g, ' python');

console.log(s);
 
  
  
   
   
/*
run:
   
"javascript python c++ PHP css"
  
*/

 



answered Feb 2, 2021 by avibootz
edited Jan 23, 2022 by avibootz
0 votes
let s = "javascript php c++ PHP css";
   
s = s.replace(/ php/gi, ' python');

console.log(s);
 
  
  
   
   
/*
run:
   
"javascript python c++ python css"
  
*/

 



answered Feb 2, 2021 by avibootz
edited Jan 23, 2022 by avibootz
0 votes
let s = "javascript php c++ PHP css";
    
s = s.split('php').join('python');

console.log(s);
  
   
   
    
    
/*
run:
    
"javascript python c++ PHP css"
   
*/

 



answered Jan 23, 2022 by avibootz
...