In this typescript tutorial, we will see how to split a string with multiple separators in typescript.
We will see different ways to split a string having multiple separators in typescript. The ways are:
- Using split() method
- Using replaceAll() method
- Using the split() and join() method
Use typescript split() method to split a string with multiple separators
Here we will see how we can use the split method to split the string having multiple separators.
With the split method, we will divide a String object into an array of strings by splitting/separating the string into sub-strings.
Syntax of the split method
string.split([separator][, limit])
In the above syntax:
- separator: This argument specifies the element to be used to separate the string.
- limit: This is an Integer that specifies the maximum number of splits that can be found.
Return:
This returns a new array of strings.
Let’s see an example of how we can implement the split method to split string multiple separators in typescript.
For example, we have a string with multiple separators, and we will separate the string using the split() method, and create a list of string arrays.
Open the code editor, create a file named ‘splitString.ts’, and write the below code.
let str = 'Can-a/d/a';
const result= str.split(/[-/_]+/);
console.log(result);//👉[ 'Can', 'a', 'd', 'a' ]
To compile the code and run the below command and you can see the result in the console:
ts-node splitString

This is an example of a Typescript split string having multiple separators using split().
Use typescript replaceAll() method to split a string having multiple separators
Here we will see how to split a string having multiple separators using replaceAll() in typescript.
The replaceAll() function in typescript creates a new string by replacing all matches of a pattern with a replacement.
The syntax of replaceAll().
replaceAll(pattern, replacement)
In the above syntax
pattern: The pattern can be regExp or object or string in typescript.
replacement: it can be a function or string, which gets called for each match
Return
This method returns a new string having all matches of the pattern.
For example, we will take a string, and then we will use replace the separator with a unified or single character using replace() in typescript.
In the splitString.ts file write the below code:
let str:any = 'Alaska-Los Angles-Carlifonia';
const result= str.replaceAll('-', '$').replaceAll(' ', '$').split('$');
console.log(result);//👉[ 'Alaska', 'Los', 'Angles', 'Carlifonia' ]
To compile the code and run the below command and you can see the result in the console.
ts-node splitString

Now let’s see how we can create reusable code using a function, which will split a string having multiple separators.
In the splitString.ts file write the below code:
function multipleSplit(str:any, separs: any) {
let strReplaced = str
for (const separator of separs) {
strReplaced = strReplaced.replaceAll(separator, '$')
}
return strReplaced.split('$')
}
const str = 'Alaska-Los Angles-Carlifonia';
console.log(multipleSplit(str, ['-'])); //👉[ 'Alaska', 'Los Angles', 'Carlifonia' ]
In the above code, multipleSplit function accepts as inputs a string and an array of separators and separates the string into an array on every incidence of a separator.
To compile the code and run the below command and you can see the result in the console.
ts-node splitString

This is an example of how we can use replaceAll() to split a string having multiple separators in a typescript.
Use typescript split() method and join () method split a string with multiple separators
Here we will see how we can split a string having multiple separators using split() and join() in typescript.
As we already discussed what is split () is and now let’s see what is join method is in typescript.
The join method in typescript will merge the string that is present in an array.
The syntax of the join() method
array.join(separator);
separator-Separator specifies a string that will be used to separate each item. If this parameter is not specified, the array items are separated by a comma.
Return
After combining all items from an array, it returns a string.
Let’s see an example of how we can split a string having multiple separators using the split() and join() method in typescript.
We will take a string and we will use the split method to split the string on each ‘-‘. We can use the join method, to join the string with the dollar ‘$’ operator, and then we will split the string by ‘$’.
In the splitString.ts file write the below code:
let str:any = 'Alaska-Los Angles-Carlifonia';
const result = str.split('-')
.join('$').split('$')
console.log(result); // 👉️[ 'Alaska', 'Los Angles', 'Carlifonia' ]
To compile the code and run the below command and you can see the result in the console:
ts-node splitString.ts

This is an example of how we can split the string having multiple separators using split() and join() in typescript.
Conclusion
In this typescript tutorial, we saw how to split the string with multiple separators in typescript with different methods.
- Using split() method
- Using replaceAll() method
- Using the split() and join() method
You may like the following typescript tutorials:
- Typescript check if a string contains a substring
- How to remove quotes from a string in Typescript
- Compare two string dates in typescript
- How to compare two strings in typescript
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