拆分日期! (实际上是一个字符串)

时间:2022-12-09 21:43:33

I want to split this kind of String :

我想拆分这种字符串:

"14:30 - 19:30" or "14:30-19:30"

inside a javascript array like ["14:30", "19:30"]

在像[“14:30”,“19:30”]这样的javascript数组中

so I have my variable

所以我有我的变量

var stringa = "14:30 - 19:30";
var stringes = [];

Should i do it with regular expressions? I think I need an help

我应该用正则表达式吗?我想我需要帮助

6 个解决方案

#1


2  

You can just use str.split :

你可以使用str.split:

var stringa = "14:30 - 19:30";
var res = str.split("-");

#2


2  

If you know that the only '-' present will be the delimiter, you can start by splitting on that:

如果您知道唯一的' - '存在将是分隔符,您可以从分裂开始:

let parts = input.split('-');

If you need to get rid of whitespace surrounding that, you should trim each part:

如果你需要摆脱周围的空白,你应该修剪每个部分:

parts = parts.map(function (it) { return it.trim(); });

To validate those parts, you can use a regex:

要验证这些部分,您可以使用正则表达式:

parts = parts.filter(function (it) { return /^\d\d:\d\d$/.test(it); });

Combined:

var input = "14:30 - 19:30";
var parts = input.split('-').map(function(it) {
  return it.trim();
}).filter(function(it) {
  return /^\d\d:\d\d$/.test(it);
});

document.getElementById('results').textContent = JSON.stringify(parts);
<pre id="results"></pre>

#3


1  

Try this :

试试这个 :

var stringa = "14:30 - 19:30";
var stringes = stringa.split("-");   // string is "14:30-19:30" this style

or

var stringes = stringa.split(" - "); // if string is "14:30 - 19:30"; style so it includes the spaces also around '-' character. 

The split function breaks the strings in sub-strings based on the location of the substring you enter inside it "-"

split函数根据你在其中输入的子字符串的位置来打破子字符串中的字符串“ - ”

. the first one splits it based on location of "-" and second one includes the spaces also " - ".

。第一个根据“ - ”的位置拆分它,第二个包括空格也是“ - ”。

*also it looks more like 24 hour clock time format than data as you mentioned in your question.

*它看起来更像24小时时钟格式而不是您在问题中提到的数据。

#4


1  

var stringa = '14:30 - 19:30';
var stringes = stringa.split("-");

#5


0  

.split is probably the best way to go, though you will want to prep the string first. I would go with str.replace(/\s*-\s*/g, '-').split('-'). to demonstrate:

.split可能是最好的方法,但你会想先准备字符串。我会选择str.replace(/ \ s * - \ s * / g,' - ')。split(' - ')。展示:

var str = "14:30 - 19:30"
var str2 = "14:30-19:30"
console.log(str.replace(/\s*-\s*/g, '-').split('-')) //outputs ["14:30", "19:30"]
console.log(str2 .replace(/\s*-\s*/g, '-').split('-')) //outputs ["14:30", "19:30"]

#6


0  

Don't forget that you can pass a RegExp into str.split

不要忘记您可以将RegExp传递给str.split

'14:30 - 19:30'.split(/\s*-\s*/); // ["14:30", "19:30"]
'14:30-19:30'.split(/\s*-\s*/); // ["14:30", "19:30"]

#1


2  

You can just use str.split :

你可以使用str.split:

var stringa = "14:30 - 19:30";
var res = str.split("-");

#2


2  

If you know that the only '-' present will be the delimiter, you can start by splitting on that:

如果您知道唯一的' - '存在将是分隔符,您可以从分裂开始:

let parts = input.split('-');

If you need to get rid of whitespace surrounding that, you should trim each part:

如果你需要摆脱周围的空白,你应该修剪每个部分:

parts = parts.map(function (it) { return it.trim(); });

To validate those parts, you can use a regex:

要验证这些部分,您可以使用正则表达式:

parts = parts.filter(function (it) { return /^\d\d:\d\d$/.test(it); });

Combined:

var input = "14:30 - 19:30";
var parts = input.split('-').map(function(it) {
  return it.trim();
}).filter(function(it) {
  return /^\d\d:\d\d$/.test(it);
});

document.getElementById('results').textContent = JSON.stringify(parts);
<pre id="results"></pre>

#3


1  

Try this :

试试这个 :

var stringa = "14:30 - 19:30";
var stringes = stringa.split("-");   // string is "14:30-19:30" this style

or

var stringes = stringa.split(" - "); // if string is "14:30 - 19:30"; style so it includes the spaces also around '-' character. 

The split function breaks the strings in sub-strings based on the location of the substring you enter inside it "-"

split函数根据你在其中输入的子字符串的位置来打破子字符串中的字符串“ - ”

. the first one splits it based on location of "-" and second one includes the spaces also " - ".

。第一个根据“ - ”的位置拆分它,第二个包括空格也是“ - ”。

*also it looks more like 24 hour clock time format than data as you mentioned in your question.

*它看起来更像24小时时钟格式而不是您在问题中提到的数据。

#4


1  

var stringa = '14:30 - 19:30';
var stringes = stringa.split("-");

#5


0  

.split is probably the best way to go, though you will want to prep the string first. I would go with str.replace(/\s*-\s*/g, '-').split('-'). to demonstrate:

.split可能是最好的方法,但你会想先准备字符串。我会选择str.replace(/ \ s * - \ s * / g,' - ')。split(' - ')。展示:

var str = "14:30 - 19:30"
var str2 = "14:30-19:30"
console.log(str.replace(/\s*-\s*/g, '-').split('-')) //outputs ["14:30", "19:30"]
console.log(str2 .replace(/\s*-\s*/g, '-').split('-')) //outputs ["14:30", "19:30"]

#6


0  

Don't forget that you can pass a RegExp into str.split

不要忘记您可以将RegExp传递给str.split

'14:30 - 19:30'.split(/\s*-\s*/); // ["14:30", "19:30"]
'14:30-19:30'.split(/\s*-\s*/); // ["14:30", "19:30"]