www.webdeveloper.com
Results 1 to 4 of 4

Thread: regular expression in Javascript

  1. #1
    Join Date
    Feb 2011
    Posts
    90

    regular expression in Javascript

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

    Code:
    '([0-9]*)x([0-9]*)'
    and for the code:
    Code:
    "400x300"
    It give me the following array:
    Code:
    [400x300,400,300]
    I understand why the first element in the array, 400x300, 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.

  2. #2
    Join Date
    Dec 2012
    Posts
    100
    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.

  3. #3
    Join Date
    Feb 2011
    Posts
    90
    iBeZi, thank you.

  4. #4
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373
    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 abc, the following examines a note:
    Code:
     //           <stc><-- accidental----><--note--><octav><num>< / ><dem><trail>
     var anote = /(\.?)(\^{0,2}|=?|_{0,2})([A-Ga-g])([',]*)(\d*)(\/*)(\d*)([- ]*)/;
    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 match 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
    HTH

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles