www.webdeveloper.com
Results 1 to 4 of 4

Thread: Regular Exp returns extra characters

  1. #1
    Join Date
    Apr 2010
    Location
    Brooklyn NY
    Posts
    104

    Regular Exp returns extra characters

    I've got an image that when it's loaded it initiates a function that runs the first block of code below which checks if the image is part of a series. If it is it searches for the number in the source file. If it's the first image (which doesn't have a number) it sets the matches to 0

    Code:
    var regex = /(\d+)/;
    matches = regex.exec(_this.source);//'files/images/transworld mag.jpg'
    alert(matches);//second time it runs it alerts '2,2'
    if (matches == null) matches = 0;
    When the next button is clicked it runs the code below which adds 1 to the matches and loads the new source file. The second image loads fine, but when it runs the above code, under matches, it alert's '2,2' when it should only be a '2'.

    Code:
    matches++;
    alert(matches);//NAN second time
    var source = _this.source.split('.');
    (matches == 1) matches = 2;
    $(obj).attr('src','');
    $(_this.fullImg).attr('src',source[0] + matches + '.' + source[1]);
    _this.source = source[0] + matches + '.' + source[1];
    //alert(source[0] + matches + '.' + source[1]);

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    var regex = /(\d+)/;
    matches = regex.exec(_this.source);//'files/images/transworld mag.jpg'
    At this point matches is an array: ["2","2"] so it makes no sense to increment it
    Code:
    matches++;
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Apr 2010
    Location
    Brooklyn NY
    Posts
    104
    I realize that but when I try to read matches[0] it returns undefined or null

  4. #4
    Join Date
    Apr 2010
    Location
    Brooklyn NY
    Posts
    104
    I realized when I alert(matches[0]) when it's not an array, it doesn't execute the rest of the code, so I made a check if it is an array now it works

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