You need the put the characters you wish to split on in a character class, which tells the regular expression engine "any of these characters is a match". For your purposes, this would look like:
date.split(/[.,\/ -]/)
Although dashes have special meaning in character classes as a range specifier (ie [a-z] means the same as [abcdefghijklmnopqrstuvwxyz]), if you put it as the last thing in the class it is taken to mean a literal dash and does not need to be escaped.
To explain why your pattern didn't work, /-./ tells the regular expression engine to match a literal dash character followed by any character (dots are wildcard characters in regular expressions). With "02-25-2010", it would split each time "-2" is encountered, because the dash matches and the dot matches "2".
You need the put the characters you wish to split on in a character class, which tells the regular expression engine "any of these characters is a match". For your purposes, this would look like:
date.split(/[.,\/ -]/)
Although dashes have special meaning in character classes as a range specifier (ie [a-z] means the same as [abcdefghijklmnopqrstuvwxyz]), if you put it as the last thing in the class it is taken to mean a literal dash and does not need to be escaped.
To explain why your pattern didn't work, /-./ tells the regular expression engine to match a literal dash character followed by any character (dots are wildcard characters in regular expressions). With "02-25-2010", it would split each time "-2" is encountered, because the dash matches and the dot matches "2".
or just (anything but numbers):
date.split(/\D/);
JavaScript String.split(RegExp) is returning empty strings
RegEx: How to Split String into Words
Complex split using Regex
How to split strings using regular expressions in JavaScript
Videos
So I have this regular expression:
/([\s+\-*\/%=&^|<>~"`!;:,.?()[\]{}\\])/gwhich should match all whitespace, and +-*/%=&|<>~"`!;:,.?()[]{}]\
(and the up caret, but reddit is being annoying)
On regex websites like this one, you can see that the Regular Expression works how I'd like it to, however if you run the JavaScript String.split, it returns all the matches I want, and the text in between matches which I also want. The issue is it also returns some empty strings, which I don't want.
Run this in your browser and you'll see what I mean:
`document.write("<h2>Table of Factorials</h2>");
for(i = 1, fact = 1; i < 10; i++, fact *= i) {
document.write(i + "! = " + fact);
document.write("<br>");
}`.split(/([\s+\-*\/%=&^|<>~"`!;:,.?()[{\]}\\])/g);The expected result is something like:
["document", ".", "write", "(", """, "<", "h2", ">", "Table", " ", "of", " ", "Factorials", "<", "/", "h2", ">", """, ")"...]however, the actual result is:
["document", ".", "write", "(", "", """, "", "<", "h2", ">", "Table", " ", "of", " ", "Factorials", "<", "", "/", "h2", ">", "", """, "", ")"...]Why am I getting some empty strings back? How can I fix it? Thank you for your time.