/    Sign up×
Community /Pin to ProfileBookmark

reg ex help …

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 ???

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@JonaApr 04.2003 — You don't use split(), you use this:

re.exec(str);
Copy linkTweet thisAlerts:
@aapunauthorApr 04.2003 — 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:
Copy linkTweet thisAlerts:
@JonaApr 04.2003 — You are using split(), you should be using:

alert(re.exec(str));
Copy linkTweet thisAlerts:
@aapunauthorApr 04.2003 — 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))

//-->
Copy linkTweet thisAlerts:
@soccer362001Apr 05.2003 — 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).
Copy linkTweet thisAlerts:
@jeffmottApr 05.2003 — [size=2]var str = "49384389749848748234343343433";
var re = new RegExp("\d{1,4}", "g");
alert(str.match(re));[/size]
Copy linkTweet thisAlerts:
@NedalsApr 05.2003 — 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 ?
×

Success!

Help @aapun spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...