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
console.log("'TEST' .. 'TEST2' ".trim().split(/\s*('|\.{2})\s*/).filter(Boolean))
Related Topics
Regular Expression Using JavaScript for Removing the Special Characters
Handling Multiple Http Responses One by One in Angular 6
Getelementbyid When Id Is Dynamically Generated
Detect If User Is Using Webview for Android/Ios or a Regular Browser
Delete Row on Remove Click React
Development Server of Create-React-App Does Not Auto Refresh
How to Clear the Focus in JavaScript
How to Detect Browser Back Button Click Event Using Angular
Foreach Is Not a Function Error With JavaScript Array
Calculate the Total of Item Quantity into Item Price With Dynamic HTML Input Fields Using Jquery
How to Tell If a String Contains a Certain Character in JavaScript
How to Make a Bot Delete Messages After a Certain Time Period
How to Compare Two Json Have the Same Properties Without Order
How to Download Fetch Response in React as File
How to Remove Forward and Backward Slashes from String in JavaScript
Pause/Stop Videos into Tabs When I Clicked on a Tab
How to Remove Single Uploaded File from Jquery in Multiple File Upload