In this typescript tutorial, we will see how to replace all occurrences of a string with a new one using a different method in typescript. Here are the methods that are going to cover in this topic:
- Typescript string replaces all occurrences
- Replace all occurrences of the string using replaceAll() in the typescript
- Replace all occurrences of a String in a case-insensitive manner in typescript
- Using replace() replace all occurrences of a String in TypeScript
- Using split() and join() replace all occurrences of a String
Replace all occurrences of a string using replaceAll() in typescript
Here we will see how we can replace all occurrences of the string using the replaceAll method in the typescript.
For example, we have a string, and we need to replace all occurrences of the old string with a new string using the replaceAll method in the typescript.
The replaceAll() method returns a new string with all occurrences of the specified substring replaced with the specified replacement string.
Syntax of replaceAll()
replaceAll(pattern, replacement)
In the code editor, create a new file name as ‘ replaceall.ts’, then write the below code.
const str:any = 'typescript is a superset of javascript';
const result = str.replaceAll('typescript', 'TS');
console.log(result); // 👉️ "TS is a superset of javascript"
To compile the code and run the below command and you can see the result in the console.
ts-node replaceall

This is an example of Replacing all occurrences of the string using replaceAll() in the typescript.
Using replaceAll() replace all occurrences of string case insensitive in typescript
Here we will see how to replace all occurrences of a string in a case-insensitive using replaceAll() in typescript.
For example, we have string case-insensitive which we will replace with the new string using replaceAll() in typescript.
As we have already seen the replaceAll() method returns a new string with all occurrences of the specified substring replaced with the specified replacement string.
In the replaceall.ts file write the below code:
const str:any = 'Typescript is a superset of javascript';
const result = str.replaceAll(/typescript/gi, 'TS');
console.log(result); // 👉️ "TS is a superset of javascript"
To compile the code and run the below command and you can see the result in the console.
ts-node replaceall.ts

This is an example of using replaceAll() to replace all occurrences of string case insensitive in typescript.
Using replace() replace all occurrences of a String in TypeScript
Here we will see how we can replace all occurrences of a string using replace() in typescript.
For example, we will use the replace() to replace all occurrences of a string with a new string in typescript.
The replace() in typescript returns a new string that replaces one, some, or all regular expression that matches with the given replacement.
Syntax of replace()
replace(regexp/substr, newSubStr/function[, flags])
In the replaceAll.ts file write the below code:
const str:any = 'Typescript is a superset of javascript';
const result = str.replace(/typescript/gi, 'TS');
console.log(result); // 👉️ "TS is a superset of javascript"
To compile the code and run the below command, you can see the result in the console.
ts-node replaceall

This is an example of Using split() and join() to replace all occurrences of a string
Using split() and join() replace all occurrences of a string
Here we will see an example using split() and join() to replace all occurrences of a string in typescript.
For example, we have a string, and we want to replace all occurrences of a string using split() and join() in typescript.
Split the string on each instance of the substring with the typescript split() function.
Syntax of split()
split([separator][, limit])
To merge the array, use the join() function with the replacement string as the separator.
Syntax of Join()
join(separator)
In the replaceAll.ts file write the below code:
const str:any = 'Typescript is a superset of javascript';
const result = str.split('Typescript').join('TS');
console.log(result); // 👉️ "TS is a superset of javascript"
To compile the code and run the below command, you can see the result in the console.
ts-node replaceall

This is an example of Using split() and join() to replace all occurrences of a string.
Conclusion
In this typescript tutorial, we saw different methods to replace all occurrences of a string in typescript. These are the method we covered.
- Replace all occurrences of the string using replaceAll() in the typescript
- Replace all occurrences of a String in a case-insensitive manner in typescript
- Using replace() replace all occurrences of a String in TypeScript
- Using split() and join() replace all occurrences of a String
You may like the following typescript tutorials:
- How to get last element of an array in typescript
- How to check if string is empty in typescript
- How to add a property to object in Typescript
- How to check type of variable in Typescript
- Data types in typescript with examples
I am Bijay a Microsoft MVP (8 times – My MVP Profile) in SharePoint and have more than 15 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com