Warning: session_start(): open(/tmp/sess_e566e62e41bc2dbc5356c5a85051159b, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Split A String With Multiple Separators Using JavaScript - LearnShareIT

How To Split A String With Multiple Separators Using JavaScript

split a string with multiple separators using JavaScript

We will learn how to split a string with multiple separators using JavaScript using two different methods. Each method is relatively straightforward, and you can choose whichever you want.

Split a string with multiple separators using JavaScript 

Using regex

Regular expressions are powerful for searching and manipulating text strings, especially when dealing with text files. A single regular expression can easily replace dozens of lines of programming code.

Syntax:

split(separator)

Parameter:

  • separator: A pattern such as a string or a regex expression that describes where to split

By declaring it in a regular expression format, you can use this function to split the string with multiple separators. This is also considered the most effective method:

let string = 'this:is,an;example.of LearnShareIT';
let array = string.split(/[.;,:\s]+/);
console.log(array)

Output:

['this', 'is', 'an', 'example', 'of', 'LearnShareIT']

The above example points out the separators are “.” or “;” or “,” or “:” and whitespace characters (“/s”) that can be repeated multiple times (as there is a sign “+” at the end of regex). The logic behind this method is very simple because if your delimiter is a non-empty string, the target string is divided by all delimiter matches, and the delimiter is not included in the result. The regex expression is placed around a slash (/) before and after. However, you may have to get acquainted with using regex before. Otherwise, you can see another approach below.

Using a for loop, split and concat

If you have no idea about using regex as we have introduced above. You can consider using our other approach of a for loop to iterate the list of separators, split the string into an array, and then merge it into one array.

Syntax:

concat(value)

Parameter:

  • value: Arrays to concatenate into a new array. 

The concat method creates a new array. The array is first filled with elements from the object that calls it:

let string = 'this:is,an;example.of LearnShareIT';
let separators = ['.',';',',',':'," "];
let res = [string];
let res1 = []

for (i of separators){
    for (j of res)
        res1 = res1.concat(j.split(i));
    res = res1;
    res1 = [];
}

console.log(res);

Output: 

['this', 'is', 'an', 'example', 'of', 'LearnShareIT']

Using this approach still produces the same result as the first one. Moreover, you will not need to learn about regex and how to write it. All you have to do is just declare your list of separators inside the “separators” array. Then run the above code you will receive an array of all substrings splitted from your string.

Summary

We have learned how to split a string with multiple separators using JavaScript using two different methods. It would help if you considered each approach’s pros and cons. By declaring your separators in regex or array list format, we can split a string with multiple separators.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *