How to Split a String With Multiple Separators in JavaScript

How do I split a string with multiple separators in JavaScript?

Pass in a regexp as the parameter:

js> "Hello awesome, world!".split(/[\s,]+/)
Hello,awesome,world!

Edited to add:

You can get the last element by selecting the length of the array minus 1:

>>> bits = "Hello awesome, world!".split(/[\s,]+/)
["Hello", "awesome", "world!"]
>>> bit = bits[bits.length - 1]
"world!"

... and if the pattern doesn't match:

>>> bits = "Hello awesome, world!".split(/foo/)
["Hello awesome, world!"]
>>> bits[bits.length - 1]
"Hello awesome, world!"

String Splitting with multiple delimiters in JavaScript

try this

"2020-01-31".split(/[/-]/ig)

var dateParts1 = "2020-01-31".split(/[/-]/ig);console.log(dateParts1);

var dateParts2 = "2020/02/21".split(/[/-]/ig);console.log(dateParts2);

javascript: split string for multiple separators but also keep the separators as separate token

You can use a capture group in your regex to preseve the tokens:

string.split(/([-+(),*/:? ])/g)
^ ^
|___ group __|

Output:

[
'elt', '(', 'a', ',',
'', ' ', 'b', ',',
'', ' ', 'c', ')',
''
]

You can also easily remove the empty strings:

string.split(/([-+(),*/:? ])/g).filter(Boolean)

RegExp / JavaScript: Split string on multiple characters, keep separators without using lookbehind

You could match the words without lookbehind.

const
string = "This is a string-thing",
parts = string.match(/.+?([\s-]|$)/g);

console.log(parts);

Split a string based on multiple delimiters

escape needed for regex related characters +,-,(,),*,?

var x = "adfds+fsdf-sdf";

var separators = [' ', '\\\+', '-', '\\\(', '\\\)', '\\*', '/', ':', '\\\?'];
console.log(separators.join('|'));
var tokens = x.split(new RegExp(separators.join('|'), 'g'));
console.log(tokens);

http://jsfiddle.net/cpdjZ/

Regex to split with multiple separators of one or several characters

The [' ] .. pattern matches a ' or space followed with a space and any two chars other than line break chars.

You may use