/    Sign up×
Community /Pin to ProfileBookmark

match and regular expressions

I was recently parsing SVG data using JavaScript String.match, and came across a problem. I was taking out coordinates to use them in a canvas application, and tried to do this:

[code]
var r = /[mc][B]([+-]?d+(.d+)?,?)[/B]+/i;
“m12.6-52.1”.match(r);
[/code]

As it’s supposed to, this just matches the last capture of the bolded group, so, just -52.1 is returned. I can’t think of an easy way around this, so I wondered if anyone else has one.

If not, I’ll probably end up leaving the regular expressions and using normal string methods to parse it, but I’d really like to use regular expressions.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@PprakashNov 13.2010 — Why you use 'mc' insted of 'm' for matching letter 'm'.

I really say I did not clearly undestand whcic h data matched
Copy linkTweet thisAlerts:
@FangNov 13.2010 — 2 matches are returned: [I]-52.1[/I] and [I].1[/I]

Can you be more specific in what should be returned?
Copy linkTweet thisAlerts:
@Declan1991authorNov 13.2010 — Why you use 'mc' insted of 'm' for matching letter 'm'.[/QUOTE]Because I'm also matching for c! That's just a sample.

@Fang, indeed I wasn't very clear. I want 12.6 and -52.1 without having to repeat myself in the regular expression.
/m([+-]?d+(.d+)?,?)([+-]?d+(.d+)?,?)/i will do what I want, but since I also want to be able to match a bezier curve which has 6 coordinates, I would rather not have to use things like this./c([+-]?d+(.d+)?,?)([+-]?d+(.d+)?,?)([+-]?d+(.d+)?,?)([+-]?d+(.d+)?,?)([+-]?d+(.d+)?,?)([+-]?d+(.d+)?,?)/i
Copy linkTweet thisAlerts:
@FangNov 13.2010 — [CODE]var r = /([+-]?[d+.?d+]+)/g;
var arr ="m12.6-52.1".match(r);
alert(arr);
[/CODE]
Copy linkTweet thisAlerts:
@Declan1991authorNov 13.2010 — That'll do fine. I'd rather test for the m or c in the same regexp all in one go, but what you have is great, thanks.
×

Success!

Help @Declan1991 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.6,
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,
)...