Typescript string replaces all occurrences

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
Replace all occurrences of the string using replaceAll() in the typescript
Replace all occurrences of the string using replaceAll() in the typescript

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
Using replaceAll() replace all occurrences of string case insensitive in typescript
Using replaceAll() replace all occurrences of string case insensitive in typescript

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
Using replace() replace all occurrences of a String in TypeScript
Using replace() replace all occurrences of a String in TypeScript

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
Using split() and join() replace all occurrences of a string
Using split() and join() replace all occurrences of a string

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:

>