/    Sign up×
Community /Pin to ProfileBookmark

regular expression in Javascript

In code that I am reading I have the following regular expression:

[CODE]‘([0-9]*)x([0-9]*)’[/CODE]

and for the code:

[CODE]“400×300”[/CODE]

It give me the following array:

[CODE][400×300,400,300][/CODE]

I understand why the first element in the array, 400×300, but how come the last two match the regular expression?

They don’t have the x.

Can someone please explain to me why the last 2 element match the regular expression.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@iBeZiMay 26.2013 — The two areas enclosed in brackets tell the regular expression that you want to "capture" whatever appears inside the brackets.

So the regular expression matches "400x300" as a whole, but then gives you the two numbers separately because they are enclosed in brackets.
Copy linkTweet thisAlerts:
@programAngelauthorMay 27.2013 — iBeZi, thank you.
Copy linkTweet thisAlerts:
@wbportMay 28.2013 — Putting parenthesis "()" around part of a regular expression is a way to determine it's value and take appropriate action. For example, in the text based music notation system [B]abc[/B], the following examines a note:[CODE]
// <stc><-- accidental----><--note--><octav><num>< / ><dem><trail>
var anote = /(.?)(^{0,2}|=?|_{0,2})([A-Ga-g])([',]*)(d*)(/*)(d*)([- ]*)/;[/CODE]

Only the upper or lower case letters "a-g" have to be there-- a stacatto dot, an accidental, octave change, duration, and/or trailing dash or space are all optional. This is an example of some code that follows a [B]match[/B] command: [CODE]
for (var i =0; i<result[4].length; i++) {
var achar = result[4].charCodeAt(i); // alert("achar = " + achar);
if (achar == 44) {pos -= 7;} // Comma down
if (achar == 39) {pos += 7;} } //Apostrophe, up[/CODE]


HTH
×

Success!

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