473,748 Members | 11,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(r egex)) {
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 1770
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*********@be tter.domain.nam e> 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(r egex)) {
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="200408 18";
var date=example.re place(/\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(r egex)) {
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.prototyp e.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).joi n("/");
PointedEars

P.S.: Nice From :)
--
Shut up and code...
Jul 23 '05 #4
Stuart Gilbert <st*********@be tter.domain.nam e> 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/rasterTriangleD OM.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**********@n ews.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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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
1926
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 2.3.4 (#2, Aug 19 2004, 15:49:40) on linux2
5
2970
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!) printing the results I expect, which I guess means my dynamic memory allocation is a mess. Also, I was advised previously that I should really free memory in the same place I declare it, but I'm not sure how I would go about doing this in my code...
4
2019
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
2522
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 fields in a table...well, someone has been entering description fields with comas (,) in the description and now it is splitting between one field...example: "santa clause mushrooms, pens, cups and dolls" I somehow need to NOT split anything...
20
3713
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 the data to assign each value to its own variable. So right now I am just saving the data to a txt file and when I look in the text file all the data is there. Not sure if this matters but when I open the text file in Word pad (Rich Text) It...
2
1490
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: Dim s As String, h As String Dim delim(1) As Char delim(0) = "/"
6
303
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 shorter strings does not seem to work with this one. Ideas? Thanks, jvh
2
3269
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 lines it worked quite well but srttok isnot working for multiple blank or commas. Can strtok do this kind of splitting if it cant what should i use . Unal
4
2824
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 integers later. I've bene looking around, and everything I see seems more complicated than it should be! Help!
37
1851
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 --srcport 131.188.37.59 --destaddress 1398 --destport tcp --protocol
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.