Splitting a string on [0-9]{2}

S

Stuart Gilbert

I'm trying to split a string on every 2 numbers. I presumed the
following would be fine, but I get nothing.



var re = /[0-9]{8}/;
var regex = new RegExp(re);
if(date.match(regex)) {
var dateArray = new Array(4);
dateArray = date.split(/[0-9]{2}/);

date = dateArray[1];
date += "/" + dateArray[2];
date += "/" + dateArray[3];
}


date begins as something like "20040818" and so it matches the first
regex but then it all goes wrong somewhere. What's the best way to do
what I'm trying to do?

Thanks in advance,

Stuart.
 
O

oeyvind toft

Perhaps this will do:


date = "20040818";

var arr = [];
i = 0;
str = "";

while (date.length > 0)
{
arr = date.substring(0, 2);
str += arr + '\n';
date = date.substring(2);
i++;
}

alert(str);


Oeyvind
 
L

Lee

Stuart Gilbert said:
I'm trying to split a string on every 2 numbers. I presumed the
following would be fine, but I get nothing.



var re = /[0-9]{8}/;
var regex = new RegExp(re);
if(date.match(regex)) {
var dateArray = new Array(4);
dateArray = date.split(/[0-9]{2}/);

date = dateArray[1];
date += "/" + dateArray[2];
date += "/" + dateArray[3];
}


date begins as something like "20040818" and so it matches the first
regex but then it all goes wrong somewhere. What's the best way to do
what I'm trying to do?

Split treats the re as the delimiter. It removes it from the result.
You don't need to declare the array before you assign it the value
of a new array.

var example="20040818";
var date=example.replace(/\d\d(\d\d)(\d{2})(\d\d)/,"$1/$2/$3");
 
T

Thomas 'PointedEars' Lahn

Stuart said:
var re = /[0-9]{8}/;
var regex = new RegExp(re);
if(date.match(regex)) {
var dateArray = new Array(4);

Not necessary. JS arrays are sized automatically.
dateArray = date.split(/[0-9]{2}/);
date = dateArray[1];
date += "/" + dateArray[2];
date += "/" + dateArray[3];

String.prototype.split() removes the delimiter from the components.
You would need to join them again, but what previously was removed
cannot be determined later. Which is why you should use
}


date begins as something like "20040818" and so it matches the first
regex but then it all goes wrong somewhere. What's the best way to do
what I'm trying to do?

date.replace(/(\d{2})/g, "/$1").substr(1);

Note that your approach is error-prone as it allows the user to use
two-digit years (not y2k-proof). You should rather use an (international)
standard date format using four-digit years, like YYYY-MM-DD. Parsing such
a format is easy:

date = date.replace(/^(\d{4})-(\d{2})-(\d{2})$/, "$1/$2/$3");
or
date = /^(\d{4})-(\d{2})-(\d{2})$/.exec(date).join("/");


PointedEars

P.S.: Nice From :)
 
L

Lasse Reichstein Nielsen

Stuart Gilbert said:
I'm trying to split a string on every 2 numbers. I presumed the
following would be fine, but I get nothing.
dateArray = date.split(/[0-9]{2}/);

When you split, you only get what is between the strings that you
split on. In your case, that's empty strings.

var dateArray = date.match(/\d\d/g);
date begins as something like "20040818" and so it matches the first
regex but then it all goes wrong somewhere. What's the best way to do
what I'm trying to do?

If you want to match a date string on the form "yyyymmdd", then
you might as well do that directly

var match = date.match(/^(\d{4})(\d\d)(\d\d)$/);
// match is undefined if format was wrong.
var date = match[1] + "-" + match[2] + "-" + match[3];

/L
/L
 
S

Stuart Gilbert

date.replace(/(\d{2})/g, "/$1").substr(1);
Note that your approach is error-prone as it allows the user to use
two-digit years (not y2k-proof). You should rather use an (international)
standard date format using four-digit years, like YYYY-MM-DD. Parsing such
a format is easy:

The date format has already been checked before I start doing that
parsing. It's only a small portion of my complete code, there are "else
if" and an "else" as well. Also, I get the date formatted by vxml and
there's nothing I can do about that. They should probably use a better
date format than that, but they don't.

Thanks a lot for your help though, and everyone else too.

Stuart.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Tue, 17 Aug 2004
19:42:55, seen in Stuart Gilbert <stu-
(e-mail address removed)> posted :
I'm trying to split a string on every 2 numbers. I presumed the
following would be fine, but I get nothing.



var re = /[0-9]{8}/;
var regex = new RegExp(re);
if(date.match(regex)) {
var dateArray = new Array(4);
dateArray = date.split(/[0-9]{2}/);

date = dateArray[1];
date += "/" + dateArray[2];
date += "/" + dateArray[3];
}


date begins as something like "20040818" and so it matches the first
regex but then it all goes wrong somewhere. What's the best way to do
what I'm trying to do?

By reading the newsgroup FAQ, thoughtfully; see below.

It points you to a site which includes date input, with/without
validation.

If your needs are as you indicate, and no more, then Lasse's answer
should be all the guidance you need.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top