I have string say "dd month yyyy" and I want split to convert to array like ["dd", " ", "month", " ", "yyyy"].
我有字符串说“dd month yyyy”,我希望拆分转换为数组,如[“dd”,“”,“month”,“”,“yyyy”]。
What I have so far and this method works. But I'm looking for Reg expression to do if someone can help?
我到目前为止,这种方法有效。但是,如果有人可以提供帮助,我正在寻找Reg表达吗?
function toArray(format) {
var vDateStr = '';
var vComponantStr = '';
var vCurrChar = '';
var vSeparators = new RegExp('[\/\\ -.,\'":]');
var vDateFormatArray = new Array();
for (var i=0; i < pFormatStr.length; i++ )
{
vCurrChar = pFormatStr.charAt(i);
if ( (vCurrChar.match(vSeparators) ) || (i + 1 == pFormatStr.length) ) // separator or end of string
{
if ( (i + 1 == pFormatStr.length) && ( !(vCurrChar.match(vSeparators) ) ) ) // at end of string add any non-separator chars to the current component
{
vComponantStr += vCurrChar;
}
vDateFormatArray.push( vComponantStr );
if ( vCurrChar.match(vSeparators) ) vDateFormatArray.push( vCurrChar );
vComponantStr = '';
}
else
{
vComponantStr += vCurrChar;
}
}
return vDateFormatArray;
}
3 个解决方案
#1
5
Simple:
简单:
> "10 Jan 2015".split(/\b/g)
< ["10", " ", "Jan", " ", "2015"]
This will split on a word boundary.
这将分裂为单词边界。
#2
3
I assume that "mm dd yyyy" will actually be numbers, but this will work for the strings as well.
我认为“mm dd yyyy”实际上是数字,但这也适用于字符串。
var date ="01 02 1292";
var dateArr = date.match(/[^\s]+|\s/g);
document.write(JSON.stringify(dateArr));
#3
1
function toArray(format) {
var r = new RegExp('([0-9]{2})( )([0-9]{2})( )([0-9]{4})');
return format.match(r).slice(1);
}
document.write(JSON.stringify(toArray("30 12 1980")));
#1
5
Simple:
简单:
> "10 Jan 2015".split(/\b/g)
< ["10", " ", "Jan", " ", "2015"]
This will split on a word boundary.
这将分裂为单词边界。
#2
3
I assume that "mm dd yyyy" will actually be numbers, but this will work for the strings as well.
我认为“mm dd yyyy”实际上是数字,但这也适用于字符串。
var date ="01 02 1292";
var dateArr = date.match(/[^\s]+|\s/g);
document.write(JSON.stringify(dateArr));
#3
1
function toArray(format) {
var r = new RegExp('([0-9]{2})( )([0-9]{2})( )([0-9]{4})');
return format.match(r).slice(1);
}
document.write(JSON.stringify(toArray("30 12 1980")));