473,324 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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

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.
Jul 23 '05 #1
6 1743
Perhaps this will do:
date = "20040818";

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

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

alert(str);
Oeyvind
--
http://home.online.no/~oeyvtoft/ToftWeb/

"Stuart Gilbert" <st*********@better.domain.name> skrev i melding
news:cf**********@news.astound.net...
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.

Jul 23 '05 #2
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");

Jul 23 '05 #3
Stuart Gilbert wrote:
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 :)
--
Shut up and code...
Jul 23 '05 #4
Stuart Gilbert <st*********@better.domain.name> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #5
> 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.
Jul 23 '05 #6
JRS: In article <cf**********@news.astound.net>, dated Tue, 17 Aug 2004
19:42:55, seen in news:comp.lang.javascript, Stuart Gilbert <stu-
as*****@better.domain.name> 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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: qwweeeit | last post by:
Splitting with RE has (for me!) misterious behaviour! I want to get the words from this string: s= 'This+(that)= a.string!!!' in a list like that considering "a.string" as a word. Python...
5
by: fatted | last post by:
I'm trying to write a function which splits a string (possibly multiple times) on a particular character and returns the strings which has been split. What I have below is kind of (oh dear!)...
4
by: JeffM | last post by:
Quick C# question: I have comma delimited values in a string array that I want to pass to seperate variables. Any tips on splitting the array? Thanks in advance! JM
2
by: Trint Smith | last post by:
Ok, My program has been formating .txt files for input into sql server and ran into a problem...the .txt is an export from an accounting package and is only supposed to contain comas (,) between...
20
by: Opettaja | last post by:
I am new to c# and I am currently trying to make a program to retrieve Battlefield 2 game stats from the gamespy servers. I have got it so I can retrieve the data but I do not know how to cut up...
2
by: CharChabil | last post by:
Using Vb.net 2005, I want to read each part in this string in an array (splitting the string) ----------- A1/EXT "BK82 LB73 21233" 105 061018 1804 ----------- That Code that i used is as follow:...
6
by: HMS Surprise | last post by:
The string s below has single and double qoutes in it. For testing I surrounded it with triple single quotes. I want to split off the portion before the first \, but my split that works with...
2
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to...
4
by: yogi_bear_79 | last post by:
I have a simple string (i.e. February 27, 2008) that I need to split into three parts. The month, day, and year. Splitting into a string array would work, and I could convert day and years to...
37
by: xyz | last post by:
I have a string 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168 for example lets say for the above string 16:23:18.659343 -- time 131.188.37.230 -- srcaddress 22 ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.