/    Sign up×
Community /Pin to ProfileBookmark

How extract certain info from a string

Javascript beginner here and I’m trying to extract an info from the string below:

;326561;[B]2[/B];79.00;event16=1;,;541535;[B]1[/B];49.50;event16=1;,;;;;event3=10.76;,;;;;event4=6.00;,;;;;event5=0.00;,;;;;event18=0.00;,;;;;event6=0.00;,;;;;event7=145.26;

specifically the bolded numbers.
so far, what i’m able to do is below:

var str=”;326561;2;79.00;event16=1;,;541535;1;49.50;event16=1;,;;;;event3=10.76;,;;;;event4=6.00;,;;;;event5=0.00;,;;;;event18=0.00;,;;;;event6=0.00;,;;;;event7=145.26;”;

var str_array = str.split(‘,’);

for (var loop=0; loop < str_array.length; loop++)
{
var loop1 = str_array[loop].split(‘;’);
document.write(loop1);
}

i don’t know how to proceed from here.
any assistance would help.
thanks!

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Mar 12.2008 — Is it just the single numbers you need?[code=php]
var str=";326561;2;79.00;event16=1;,;541535;1;49.50;event16=1;,;;;;event3=10.76;,;;;;event4=6.00;,;;;;ev ent5=0.00;,;;;;event18=0.00;,;;;;event6=0.00;,;;;;event7=145.26;";

var str_array = str.split(',');

for (var loop=str_array.length; loop >= 0; loop--)
{
var loop1 = str_array[loop].split(';');
if (/d/.test(loop1)) {
document.write(loop1);
}
}
[/code]
Copy linkTweet thisAlerts:
@doogermanauthorMar 12.2008 — just those two numbers and hopefully be able to add them.

i came-up with this:

var add1 = loop1[2].split('^')+ ' ';
document.write(add1);


and placed it just below the var loop1 and got the results.

but i don't know how the add them.

I also noticed invisible characters after the numbers which I don't want.
Copy linkTweet thisAlerts:
@doogermanauthorMar 12.2008 — declan1991

could you explain what you did with the code you posted?

thanks!
Copy linkTweet thisAlerts:
@mrhooMar 12.2008 — You can do it from the string by matching the digits you are looking for which follow and are preceded by a semicolon-

var str= ";326561;2;79.00;event16=1;,;541535;1;49.50;event16=1;,;;;;event3=10.76;,;;;;event4=6.00;,;;;;ev ent5=0.00;,;;;;event18=0.00;,;;;;event6=0.00;,;;;;event7=145.26;";


var total= 0,M= true;

var Rx= /;([12]);/g;

while((M= Rx.exec(str))!=null) total+= parseInt(M[1]);

alert(total)
Copy linkTweet thisAlerts:
@doogermanauthorMar 12.2008 — thanks!
Copy linkTweet thisAlerts:
@Ay__351_eMar 12.2008 — 
&lt;script type="text/javascript"&gt;

var str=";326561;2;79.00;event16=1;,;541535;1;49.50;event16=1;,;;;;event3=10.76;,;;;;event4=6.00;,;;;;ev ent5=0.00;,;;;;event18=0.00;,;;;;event6=0.00;,;;;;event7=145.26;";
var s=str.split(',');

var total=0;
for(var i=0; s[i].split(';')[2]; i++) total += Number(s[i].split(';')[2]);
alert(total)

&lt;/script&gt;
×

Success!

Help @doogerman 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.16,
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,
)...