简体   繁体   中英

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 PM" 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 PM

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM