Last Update - 28 Jul 2020

Split a string with multiple separators in javascript

Split a string with multiple separators in javascript

How do I split a string with multiple separators in JavaScript? Are you trying to split into both commas and spaces and other things? But not getting success!. Don't worry, I'll give you the best solution for you.

If you want to split a string with multiple separators then you have to pass regex expression into a split method. In the below example I have used three characters (,;:_ -) as a separator.

JavaScript split Examples

Separate parts from a string with the split function. Split into characters, strings, and patterns.

Consider a string that has many parts. It has a delimiter character that we want to split upon. With split() in JavaScript, we have many ways to do this.

Top 10 PHP Tips And Tricks For Beginners

When found, a separator is removed from the string, and the substrings are returned in an array. If a separator is a regular expression with capturing parentheses, then each time separator matches, the results of the capturing parentheses are spliced into the output array.

Removing spaces from a string

In the following example, split() looks for spaces, if either is a single or more than one spaces are there. When found, removes the spaces from the string. Then the name list is the array returned as a result of split().

Input

var snippets = "In the following example, split() looks for spaces";
var strings = snippets.split(' ');
strings.forEach(myFunction);

function myFunction(item, index) {
  document.getElementById("result").innerHTML += index + ":" + item + "<br>";
}

Output

In
the
following
example,
split()
looks
for
spaces

Split a string with multiple separators

Sometimes we need a more complex approach to separating strings. We can use a regular expression. Here we split on one or more non-word characters like spaces or newlines or any other else.

Explode string by last occurrence and get first part

Input

<html>
<head>
<title>Split a string with multiple separators in javascript</title>
</head>

<body>
<div id="snippets">Name and territory of the; Union.—(1) India, that is Bharat, shall be a Union of States.</div>
<br /><br />
<div id="result"></div>
<script>

var snippets = document.getElementById("snippets").innerHTML;
var strings = snippets.split(/[;,—]+/);
strings.forEach(myFunction);

function myFunction(item, index) {
  document.getElementById("result").innerHTML += index + ":" + item + "<br>";
}

</script>

</body>
</html>

Output

Name and territory of the; Union.—(1) India, that is Bharat, shall be a Union of States.

0:Name and territory of the
1: Union.
2:(1) India
3: that is Bharat
4: shall be a Union of States.

With a regular expression, we can treat multiple delimiters as a single one. This eliminates empty entries.

The best approach is the simplest. With a single character delimiter, we can write simple code to split the string. Sometimes a regular expression is needed.

Did you find this page helpful? X