1

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.

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

then time will be an array containing ["10", "36", "P.M."] which you can access like time[0] to get the "10", and I post this because the original poster seems like he wouldn't figure this out with his current knowledge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.