Click to See Complete Forum and Search --> : reg ex help ...
aapun
04-04-2003, 04:24 PM
I want to split a string of numbers into an array of four characters each...
This is what i did:
<SCRIPT LANGUAGE="JavaScript">
<!--
str="49384389749848748234343343433"
re = new RegExp("[0-9]{1,4}", "g");
alert(str.split(re))
//-->
</SCRIPT>
returns nothing :(
Any feedback ???
You don't use split(), you use this:
re.exec(str);
aapun
04-04-2003, 04:33 PM
Jona,
thank you for the lead.
I tried this as per your suggestion.
alert(str.split(re))
It shows only the first four characters. I dont get the entire array. Am i missing something in my reg ex ??? Do i need to mention a star somewhere. How do i make him split after the first four characters ?? :rolleyes:
You are using split(), you should be using:
alert(re.exec(str));
aapun
04-04-2003, 04:54 PM
oops my bad, typo !!
I tried this... it displays only the first 4 chars
<!--
str="49384389749848748"
re = new RegExp("[0-9]{1,4}", "g");
//alert(str.split(re))
alert(re.exec(str))
//-->
soccer362001
04-04-2003, 09:09 PM
This part: {1,4} is what makes it only allowed to go through the first 4 characters. Take that out and see what happens.
P.S. This is Jona (not at home).
jeffmott
04-04-2003, 09:47 PM
var str = "49384389749848748234343343433";
var re = new RegExp("\\d{1,4}", "g");
alert(str.match(re));
Nedals
04-04-2003, 09:59 PM
Bit long-winded, but it works.
myRe=/\d{4}/g;
str="49384389749848748";
strlen = Math.ceil(str.length/4)
var Ary = new Array();
for (i=0; i<strlen; i++) {
Ary[i] = myRe.exec(str);
alert(Ary[i]);
}
There must be a better way!! :)
jeff post appeared after I posted. I knew there was a better way :D