How to implement a replaceAll function in TypeScript

1 Answer

0 votes
function replaceAll(str: string, find: string, replace: string) {
	return str.replace(new RegExp(find, 'g'), replace);
}

let str: string = "typescript typescript c c++ typescript c# java python";
const tofind: string = "typescript";

str = replaceAll(str, tofind, ''); 
 
console.log(str); 

 
 
/*
run:
 
"  c c++  c# java python" 
 
*/

 



answered Oct 13, 2024 by avibootz
...