How to remove all occurrences of word from a string in TypeScript

2 Answers

0 votes
let s: string = "typescript java c c++ java c# java php python";
const toremove: string = "java";

const regex: RegExp = new RegExp(toremove, 'g'); 
s = s.replace(regex, ''); 
 
console.log(s); 

 
 
/*
run:
 
"typescript  c c++  c#  php python" 
 
*/

 



answered Oct 13, 2024 by avibootz
edited Oct 13, 2024 by avibootz
0 votes
function replaceAll(str: string, find: string, replace: string) {
	return str.replace(new RegExp(find, 'g'), replace);
}

let s: string = "typescript java c c++ java c# java php python";
const toremove: string = "java";

s = replaceAll(s, toremove, ''); 
 
console.log(s); 

 
 
/*
run:
 
"typescript  c c++  c#  php python" 
 
*/

 



answered Oct 13, 2024 by avibootz
...