- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return a splitted array of the string based on all the specified separators - JavaScript
We are required to write a JavaScript function that takes in a string and any number of characters specified as separators. Our function should return a splitted array of the string based on all the separators specified.
For example −
If the string is −
const str = 'rttt.trt/trfd/trtr,tr';
And the separators are −
const sep = ['/', '.', ','];
Then the output should be −
const output = [ 'rttt', 'trt', 'trfd', 'trtr' ];
Example
Following is the code −
const str = 'rttt.trt/trfd/trtr,tr'; const splitMultiple = (str, ...separator) => { const res = []; let start = 0; for(let i = 0; i < str.length; i++){ if(!separator.includes(str[i])){ continue; }; res.push(str.substring(start, i)); start = i+1; }; return res; }; console.log(splitMultiple(str, '/', '.', ','))
Output
This will produce the following output on console −
[ 'rttt', 'trt', 'trfd', 'trtr' ]
- Related Articles
- Splitting strings based on multiple separators - JavaScript
- Shuffling string based on an array in JavaScript
- Shifting string letters based on an array in JavaScript
- Constructing a string based on character matrix and number array in JavaScript
- Forming and matching strings of an array based on a random string in JavaScript
- Change string based on a condition - JavaScript
- Return an array of all the indices of minimum elements in the array in JavaScript
- Sorting Array based on another array JavaScript
- Returning acronym based on a string in JavaScript
- How to return a new string with a specified number of copies of an existing string with JavaScript?
- Replace all occurrence of specific words in a sentence based on an array of words in JavaScript
- Build maximum array based on a 2-D array - JavaScript
- MySQL query to return the entire date and time based on a string and format
- Sort object array based on another array of keys - JavaScript
- Sort array based on another array in JavaScript

Advertisements