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/);
Videos
Use a non-capturing group as split regex. By using non-capturing group, split matches will not be included in resulting array.
var string4 = 'one split two splat three splot four';
var splitString4 = string4.split(/\s+(?:split|splat|splot)\s+/);
console.log(splitString4);
// Output => ["one", "two", "three", "four"]
If you want to use match you can write it like
'one split two split three split four'.match(/(\b(?!split\b)[^ $]+\b)/g)
["one", "two", "three", "four"]
What it does?
\bMatches a word boundary(?!split\b)Negative look ahead, check if the word is notsplit[^ $]+Matches anything other than space or$, end of string. This pattern will match a word, the look ahead ensures that what it matches is notsplit.\bMatches the word end.
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.