Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
109
rated 0 times [  115] [ 6]  / answers: 1 / hits: 22593  / 12 Years ago, fri, march 16, 2012, 12:00:00

I am trying to split following (or similar) string 08-27-2015 07:25:00AM.
Currently I use



var parts = date.split(/[^0-9a-zA-Z]+/g);


Which results in



[02, 27, 2012, 03, 25, 00AM]


The problem is with the 00AM part. I want it to be separated too. So the perfect result would be:



[02, 27, 2012, 03, 25, 00, AM]

More From » regex

 Answers
80

If you're looking for sequences of letters or numbers, but not a sequence both mixed, you can do this...



08-27-2015 07:25:00AM.match(/[a-zA-Z]+|[0-9]+/g)


resulting in...



[08, 27, 2015, 07, 25, 00, AM]


On either side of the |, we have a sequence of one or more letters and a sequence of one or more numbers. So when it comes across a letter, it will gather all contiguous letters until it reaches a non-letter, at which point it gathers all contiguous numbers, and so on.



Any other character simply doesn't match so it doesn't become part of the result.


[#86795] Thursday, March 15, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deonkalvinw

Total Points: 409
Total Questions: 96
Total Answers: 89

Location: Saint Pierre and Miquelon
Member since Sun, Nov 27, 2022
2 Years ago
deonkalvinw questions
Sun, Feb 6, 22, 00:00, 2 Years ago
Tue, Dec 28, 21, 00:00, 2 Years ago
Sun, Aug 22, 21, 00:00, 3 Years ago
Sun, Mar 7, 21, 00:00, 3 Years ago
;