简体   繁体   English

如何将字符串沿着Javascript中的某些字符分成多个字符串

[英]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". 我是编程新手,所以如果答案很简单,这将对您有所帮助,但是在Javascript中,我有一个名为“ 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 例如,它可能是“ 10:36 PM”,我想将其沿结肠和空间分开,以使其成为三个不同的变量:10、36和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 | :只是一个冒号, \\s表示一个空格字符, |表示空格| between them is special in regular expressions and basically means or . 它们之间的区别是正则表达式的特殊之处,基本上是指or

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用 javascript 分割多个字符 - How split multiple characters with javascript 正则表达式的逻辑,可将Javascript中的字符串拆分为多个字符串 - Logic of Regex to split string in Javascript to multiple strings 根据特殊字符和换行符将字符串拆分为多个字段 - Javascript - Split strings into multiple fields based on special characters and newline - Javascript 用 <br> 并限制为Javascript或JQuery中的某些字符? - Split string with <br> and limit to certain number of characters in Javascript or JQuery? 将字符串拆分为字符数组,并保持一定的顺序在一起(JavaScript) - Split string into array of characters, keep certain sequence together (JavaScript) 在 Javascript 中的一定数量的字符之后在空格处拆分字符串 - Split string at space after certain number of characters in Javascript Codewars Challenge - The Deaf Rats of Hamelin - JavaScript - 如何按特定字符集拆分字符串? - Codewars Challenge - The Deaf Rats of Hamelin - JavaScript - How to Split a String by a Certain Set of Characters? 如何在javascript中的一定数量的字符后在空格中拆分字符串? - How do I split a string at a space after a certain number of characters in javascript? 如何在javascript中将一个字符串拆分为两个字符串? - how to split a string in two strings in javascript? 如何分割javascript字符串,仅保留字符 - how to split javascript string, leaving just the characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM