SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2001
    Location
    Grosse Pointe Farms, MI
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regular espressions and .match()

    Ok, I have a regular expression called mCheck and a variable called usrVal which contains '1/20/41/11/22'
    I then use the usrVal.match(mCheck) so the code looks like the following

    var mCheck = /^[0-9][0-9]/;
    if(!usrVal.match(mCheck))
    {
    usrVal = '0' + usrVal;
    }

    Now, I was under the impression that if there wasn't a match then the match method would return boolean 'False'.

    However it is returning 'null' instead...

    I have used this exact same check on other pages before and it returned false just fine....

    Any ideas?

    -Ryan
    Michigan looks like your left hand . . . half way between your wrist and the base of your thumb is where I live.

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2001
    Location
    Grosse Pointe Farms, MI
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the error is "object expected"

    however, I know usrVal and mCheck both values since I put an alert right above them!

    -Ryan
    Michigan looks like your left hand . . . half way between your wrist and the base of your thumb is where I live.

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2001
    Location
    Grosse Pointe Farms, MI
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh I feel like an idiot!

    in the actual code instead of:

    if(!usrVal.match(monthCheck))

    I did

    If(!....)


    -Ryan
    Michigan looks like your left hand . . . half way between your wrist and the base of your thumb is where I live.

  4. #4
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I’m glad you figured it out, but it seems you are using the wrong method if you just want to return a TRUE or FALSE value.

    According to MS documentation, .match ( ) “Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.”

    You probably don’t need the array, so try using .test ( ) instead, ie:
    Code:
    var mCheck = /^[0-9][0-9]/;
    if ( ! mCheck.test ( usrVal ) ) {
      usrVal = '0' + usrVal;
    }
    You could also use this, slightly shorter expression:
    Code:
    /^\d{1,2}/
    Last edited by Mr. Brownstone; Jan 21, 2003 at 09:17.
    Of course, that's just my opinion. I could be wrong.

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Mr. Brownstone
    You could also use this, slightly shorter expression:
    Code:
    /^\d{1,2}/
    Or even shorter
    Code:
    /^\d\d/
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  6. #6
    SitePoint Zealot
    Join Date
    Jul 2001
    Location
    Grosse Pointe Farms, MI
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Mr. Brownstone
    ...According to MS documentation, .match ( ) “Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.”

    You probably don’t need the array, so try using .test ( ) ...
    Thanks for the help.

    I didn't know about .test()... I used .match since it returns false if not found and I just check if false (anything else won't trigger a false). I'll have to change that to .test

    Thanks again,
    -Ryan
    Michigan looks like your left hand . . . half way between your wrist and the base of your thumb is where I live.

  7. #7
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, better to use test.

    However, make sure you are aware of the fact that test is a method of a RegExp, and not a String.

    RegExp.test( String );

    NOT

    String.test( RegExp );

    If you'd prefer to use a String method, you can use search()

    String.search( RegExp );

    but that works a little differently. A true/false value can be determined from the results, but it actually works like indexOf() in that it returns the index of the match, or a -1 if there is no match. What makes indexOf() different from search() is that indexOf() can't take a pattern for an argument, it must be

    String.indexOf( String );

    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  8. #8
    SitePoint Enthusiast Analyser's Avatar
    Join Date
    Oct 2002
    Location
    Delft, The Netherlands
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to avoid any possible confusion:

    You could also use this, slightly shorter expression:
    Code:
    /^\d{1,2}/
    That regex will not match two digits, rather either one or two. Using interval notation, the regex you're looking for would be

    Code:
    /^\d{2,2}/
    Or, if JS allows the notation (sorry, I forget):

    Code:
    /^\d{2}/
    Just my €0.02
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/

  9. #9
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Analyser
    Or, if JS allows the notation (sorry, I forget):
    Most certainly does. And good eye on the 1,2 quantifier there Analyser (hey, you have to earn your name, right?!?)

    \d\d == \d{2}
    \d\d? == \d{1,2}
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  10. #10
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Analyser
    That regex will not match two digits, rather either one or two.
    Yes I know, but if you look at his input string ('1/20/41/11/22') you can see that there is a single-digit number, which I thought he was trying to match also, hence the suggested {1,2}.

    But then, if he is only trying to add a leading-zero to the first-number (note the ^ ; I missed this first time round *slaps head*), then surely he should only be matching a single digit, ala /^\d/ ? Also, the "^" and lack of /g leaves him pretty buggered if he wants to check all the numbers for leading-zeros.

    I think this would be a better solution:

    Code:
    var sInput = '1/2/3/4/21/32/45/65/4/6';
    
    // Note: \b Matches a word-boundary
    sInput = sInput.replace ( /\b(\d)\b/g, '0$1' );
    This gives the result '01/02/03/04/21/32/45/65/04/06'.
    Of course, that's just my opinion. I could be wrong.


Bookmarks

Posting Permissions

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