How To Split A String With Multiple Separators Using JavaScript

split a string with multiple separators using JavaScript

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:

Leave a Reply

Your email address will not be published. Required fields are marked *