Splitting a string based on multiple char delimiters in Javascript
The str.split() function is used to split the given string into array of strings by separating it into substrings using a specified separator provided in the argument.
The Array.join() function is used to join the elements of the array together into a string. This method adds the elements of an array into a string and returns the newly created string. The elements separates by a specific separator. Default separator used is comma (, ).
Example 1
<!DOCTYPE html>
<html>
<head>
<title>
JavaScript | Split a string with
multiple separators.
</title>
</head>
<body style = "text-align:center;" id = "body">
<h1 style = "color:green;" >
GeeksForGeeks
</h1>
<p id="GFG_UP" style="font-size:18px; font-weight:bold;"></p>
<button onclick = "gfg_Run()">
split
</button>
<p id="GFG_DOWN" style="font-size:25px; font-weight:bold;"></p>
<script>
var el_up = document.getElementById("GFG_UP");
var el_down = document.getElementById("GFG_DOWN");
var str = "A, computer science, portal!";
el_up.innerHTML = str;
function gfg_Run() {
var arr = str.split(/[\s, ]+/)
el_down.innerHTML = arr;
}
</script>
</body>
</html>
Output

Example 2
<!DOCTYPE html>
<html>
<head>
<title>
JavaScript | Split a string with
multiple separators.
</title>
</head>
<body style = "text-align:center;" id = "body">
<h1 style = "color:green;" >
GeeksForGeeks
</h1>
<p id = "GFG_UP" style = "font-size: 18px; font-weight: bold;">
</p>
<button onclick = "gfg_Run()">
split
</button>
<p id = "GFG_DOWN" style = "font-size: 25px; font-weight: bold;">
</p>
<script>
var el_up = document.getElementById("GFG_UP");
var el_down = document.getElementById("GFG_DOWN");
var str = "A, computer=science:portal!";
el_up.innerHTML = str;
function gfg_Run(){
var arr =
str.split('=').join(', ').split(':').join(', ').split(', ');
el_down.innerHTML = arr;
}
</script>
</body>
</html>
Output
