/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] match() problem…

I have a var vTotal with the string value of “$120.00 USD”, I am trying to get “120.00” out of that string. (The string will be dynamic.)

I am trying to use:
[COLOR=DarkRed]alert[/COLOR][COLOR=Navy][B]([/B][/COLOR]vTotal[B][COLOR=Navy])[/COLOR][/B];
[COLOR=Gray]// Sends An Alert Of “$120.00 USD”[/COLOR]

[COLOR=DarkRed]alert[/COLOR][B][COLOR=Navy]([/COLOR][/B]vTotal.[COLOR=SeaGreen]match[/COLOR][B][COLOR=Navy]([/COLOR][/B][COLOR=DarkGreen]/^d{1,3}(,d{3})*.dd$|^d+.dd$/[/COLOR][COLOR=Navy][B])[/B][/COLOR].test[B][COLOR=Navy]())[/COLOR][/B];
[COLOR=Gray]// No Alert Is Shown
// FireFox Error Console Shows That It’s Null[/COLOR]

[B]Firefox Error:[/B]
Error: vTotal.[COLOR=SeaGreen]match[/COLOR][COLOR=Navy][B]([/B][/COLOR][COLOR=DarkGreen]/^d{1,3}$/[/COLOR][B][COLOR=Navy])[/COLOR][/B] is null

I am pretty new to JavaScript but am familiar with xHTML/CSS and PHP. However, I’d appreciate a detailed response as to why it may not be working and what can be done to fix it.

Thanks ?

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@mrhooJul 13.2008 — [CODE]var s='$120.00 usd';
var M= s.match(/^(D*)((.d+)|d+(.d+)?)(D*)$/);

if(M)alert(M[2]);
else alert('No digits found');[/CODE]




f you do not care about the non-digit characters,

you can simplify the regexp to /((.d+)|d+(.d+)?)/

and examine the first (M[1]) match position
Copy linkTweet thisAlerts:
@Declan1991Jul 13.2008 — "$120.00 USD".match(/d+.d+?/gi);

There should be no test() after the match, and ^ marks the first in the string, so the $ will stop that much.

EDIT: I was beaten to it.
Copy linkTweet thisAlerts:
@ShibStaauthorJul 13.2008 — Thanks for the response.

I just tried:

[COLOR=DarkRed]alert[/COLOR][COLOR=Navy][B]([/B][/COLOR]vTotal.[COLOR=SeaGreen]match[/COLOR][B][COLOR=Navy]([/COLOR][/B][COLOR=DarkGreen]/d{1,3}(,d{3})*.dd$|^d+.dd/[/COLOR][COLOR=Navy][B]))[/B][/COLOR];

and

[COLOR=DarkRed]alert[/COLOR][COLOR=Navy][B]([/B][/COLOR]vTotal.[COLOR=SeaGreen]match[/COLOR][COLOR=Navy][B]([/B][/COLOR][COLOR=DarkGreen]/((.d+)|d+(.d+)?)/[/COLOR][B][COLOR=Navy]))[/COLOR][/B];

Though they are showing an alert now, the alert says "null".

Thanks again. ?
Copy linkTweet thisAlerts:
@mrhooJul 13.2008 — alert(vTotal)
Copy linkTweet thisAlerts:
@ShibStaauthorJul 13.2008 — Erm, relevance?

alert(vTotal) will only output "$120.00 USD", as I mentioned in my initial post.

Thanks for trying to help though.
Copy linkTweet thisAlerts:
@Declan1991Jul 13.2008 — The second works for me.

Butvar vTotal = "$120.00 USD";
alert(vTotal.match(/d{1,3}(,d{3})?(.d+)?/)[0]);
is probably what I'd use.
Copy linkTweet thisAlerts:
@mrhooJul 14.2008 — The point is that if vTotal contains any digits, either match would find them.

var vTotal='$120.00';

var M=vTotal.match(/((.d+)|d+(.d+)?)/)

alert(M[1]);
×

Success!

Help @ShibSta 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.29,
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,
)...