MOCKSTACKS
EN
Questions And Answers

More Tutorials




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




Conclusion

In this page (written and validated by ) you learned about . What's Next? If you are interested in completing Javascript tutorial, we encourage you simply to start here: Javascript Tutorial.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.