We will learn how to split a string with multiple separators using JavaScript using two different methods. Each method is relatively straightforward, and you can choose whichever you want.
Split a string with multiple separators using JavaScript
Using regex
Regular expressions are powerful for searching and manipulating text strings, especially when dealing with text files. A single regular expression can easily replace dozens of lines of programming code.
Syntax:
split(separator)
Parameter:
- separator: A pattern such as a string or a regex expression that describes where to split
By declaring it in a regular expression format, you can use this function to split the string with multiple separators. This is also considered the most effective method:
let string = 'this:is,an;example.of LearnShareIT';
let array = string.split(/[.;,:\s]+/);
console.log(array)
Output:
['this', 'is', 'an', 'example', 'of', 'LearnShareIT']
The above example points out the separators are “.” or “;” or “,” or “:” and whitespace characters (“/s”) that can be repeated multiple times (as there is a sign “+” at the end of regex). The logic behind this method is very simple because if your delimiter is a non-empty string, the target string is divided by all delimiter matches, and the delimiter is not included in the result. The regex expression is placed around a slash (/) before and after. However, you may have to get acquainted with using regex before. Otherwise, you can see another approach below.
Using a for loop, split and concat
If you have no idea about using regex as we have introduced above. You can consider using our other approach of a for loop to iterate the list of separators, split the string into an array, and then merge it into one array.
Syntax:
concat(value)
Parameter:
- value: Arrays to concatenate into a new array.
The concat method creates a new array. The array is first filled with elements from the object that calls it:
let string = 'this:is,an;example.of LearnShareIT';
let separators = ['.',';',',',':'," "];
let res = [string];
let res1 = []
for (i of separators){
for (j of res)
res1 = res1.concat(j.split(i));
res = res1;
res1 = [];
}
console.log(res);
Output:
['this', 'is', 'an', 'example', 'of', 'LearnShareIT']
Using this approach still produces the same result as the first one. Moreover, you will not need to learn about regex and how to write it. All you have to do is just declare your list of separators inside the “separators” array. Then run the above code you will receive an array of all substrings splitted from your string.
Summary
We have learned how to split a string with multiple separators using JavaScript using two different methods. It would help if you considered each approach’s pros and cons. By declaring your separators in regex or array list format, we can split a string with multiple separators.
Maybe you are interested:
- Remove Substring From String in JavaScript
- Remove everything after specific Character in JavaScript
- Capitalize the First Letter of Each Word in JavaScript
- How to trim a String to N characters in JavaScript

I’m Edward Anderson. My current job is as a programmer. I’m majoring in information technology and 5 years of programming expertise. Python, C, C++, Javascript, Java, HTML, CSS, and R are my strong suits. Let me know if you have any questions about these programming languages.
Name of the university: HCMUT
Major: CS
Programming Languages: Python, C, C++, Javascript, Java, HTML, CSS, R