JavaScript | Split a string with multiple separators
Given a statement which contains the string and separators, the task is to split the string into substring.
String split() Method: 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.
Syntax:
str.split(separator, limit)
paramters: This function accepts three parameters as mentioned above and described below:
- str: This parameter holds the string to be split.
- separator: It is optional parameter. It defines the character or the regular expression to use for breaking the string. If not used, the same string is returned (single item array).
- limit: It is optional parameter. It is an integer which specifies the number of splits. All the items after limit are discarded.
Array join() function: 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 (, ).
Syntax:
array.join(separator)
paramters: This function accepts two parameters as mentioned above and described below:
- array: The array to be joined.
- separator: It is optional parameter. It specifies the separator to be used. If not passes, the default separator comma is used.
Example 1: This example splits a string by 2 separators Comma(, ) and space(‘ ‘) using .split() function.
<!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:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: This example split the string by number of separators like Comma(, ), equal(=) and colon(:) using multiple .join() and .split() method.
<!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:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- JavaScript | String split()
- Create a string with multiple spaces in JavaScript
- Split an array into chunks in JavaScript
- JavaScript | Replace multiple strings with multiple other strings
- Round off a number to the next multiple of 5 using JavaScript
- How to put string in array, split by new line in PHP ?
- JavaScript | Return multiple values from function
- Split a comma delimited string into an array in PHP
- Call multiple JavaScript functions in onclick event
- JavaScript | Difference between String.slice and String.substring
- JavaScript | Check if a string is a valid JSON string
- JavaScript | Insert a string at position X of another string
- How to count string occurrence in string using JavaScript?
- JavaScript | String concat()
- JavaScript | String endsWith()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



