CPC
CPC

Reputation: 181

How to split string into multiple strings along certain characters in Javascript

I am new at programming so it would be help if the answers very simple, but in Javascript I have a variable called "time". It might be "10:36 P.M." for example, and I want to split it along the colon and the space, so that it becomes the three different variables: 10, 36, and P.M.

I know how to cut off the end or beginning of variables, but I need something that will save all of the parts as a separate variable.

Upvotes: 1

Views: 384

Answers (1)

Bill Criswell
Bill Criswell

Reputation: 32921

You can do.

time = time.split(/:|\s/);

Let me explain what's going on inside the split call.

The / at the start and the end means it's a regular expression. The : is just a colon, \s means a space character, the | between them is special in regular expressions and basically means or.

Upvotes: 6

Related Questions