/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Regular Expression

Can somene clarify to me why does the following code:

[code]
var str = “Oranges, 12.34$, Apples, 21.15$, Bananas, 512.44$, Plums”
var regExp = /(, d+.d+.,)/g;
var myarray = str.split(regExp);
var index;

for (index in myarray)
{
document.write(myarray[index] + “<br />”);
}
[/code]

produces following output:

[b]Oranges
, 12.34$,
Apples
, 21.15$,
Bananas
, 512.44$,
Plums[/b]

while:

[code]
var regExp = /(?:, d+.d+.,)/g;
[/code]

or

[code]
var regExp = /, d+.d+.,/g;
[/code]

produces:

[b]Oranges
Apples
Bananas
Plums[/b]

I know that [B]()[/B] are capturing parentheses and [B](?:)[/B] are non-capturing one but how does this exactly apply to the code above???

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@tech_soul8authorSep 14.2013 — I think I have just found the answer:

If separator is a regular expression that contains capturing parentheses, then each time separator is matched the results (including any undefined results) of the capturing parentheses are spliced into the output array. However, not all browsers support this capability.[/QUOTE]
Copy linkTweet thisAlerts:
@PadonakSep 15.2013 — However, not all browsers support this capability[/QUOTE]

looks like a hidden trap
Copy linkTweet thisAlerts:
@rootSep 15.2013 — Whats wrong with using the more simpler [CODE]arrayVar = str.split(",");[/CODE]
Copy linkTweet thisAlerts:
@PadonakSep 15.2013 — too simple
Copy linkTweet thisAlerts:
@tech_soul8authorSep 15.2013 — Whats wrong with using the more simpler [CODE]arrayVar = str.split(",");[/CODE][/QUOTE]

Then fruit prices would be also stored in an array and I wanted to remove prices from string and store just fruits.
Copy linkTweet thisAlerts:
@PadonakSep 15.2013 — <i>
</i>
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="content-type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;RE&lt;/title&gt;
&lt;script&gt;
var str="Oranges, 12.34$, Apples, 21.15$, Bananas, 512.44$, Plums",re=/[d.$]{1,}/g;
str=str.replace(re,'').replace(/s/g,'');
var myarray=str.split(',');
for(var i in myarray){
if(myarray[i].length&gt;0)document.write(myarray[i] + "&lt;br /&gt;");
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;&lt;/body&gt;
&lt;/html&gt;

Copy linkTweet thisAlerts:
@MrRedJan 07.2015 — sorry posted to wrong thread
Copy linkTweet thisAlerts:
@rootJan 08.2015 — How much are your plums ? You have no price for them...
×

Success!

Help @tech_soul8 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 4.23,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...