Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Nov 2004
    Location
    near the only active volcano in the continental US
    Posts
    409
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Question Match() Method Clarification

    I understand that using the Match() Method when checking a string returns Null if no match is found, however, I want to make sure that I am using it properly in an IF statement as follows;

    if (stringOBJ.match("value")) //if true
    {
    do this part of the function
    }
    else //if false
    {
    do this part of the function instead
    }

    Basicly what I want to do is make sure at least one instance of the "value" exists in the string object before I move on to a Split() method to break the string down. What I have read on the subject is a bit confusing since one of my references says that if the match is true it returns an array of information while another reference doesn't say anything about what is returned. Assuming that no match means null(false) is returned it would seem logical that a match would return true, or am I misunderstanding this?

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 250 Times in 246 Posts
    If a match is found, it returns an array of the matched strings, otherwise, it returns null.
    Code:
    var arr = stringOBJ.match("value");
    if (arr){ //arr is not null, so there's a match
      alert(arr);
    }
    else{ //arr is null, so no match
      alert("no match");
    }
    You may specify a regular expression as parameter.
    This is useful especially if you want it to match without case sensitivity
    Code:
    var arr = stringOBJ.match(/value/i); //i means ignore case
    Since you're not specifying "g" flag to perform a global match, it will only return an array with only a single match which is what you need.

  • #3
    gph
    gph is offline
    Regular Coder
    Join Date
    Apr 2005
    Posts
    161
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Assuming "value" is in the string:

    if you use:

    stringOBJ.match(/value/g)

    it returns an array of all the matches which will evaluate to true

    if you use:

    stringOBJ.match("value")

    it returns the first match which will also evaluate to true

    try this

    var str='abc';

    if(str)alert('true');

    str='';

    if(!str)alert('str is empty');

  • #4
    gph
    gph is offline
    Regular Coder
    Join Date
    Apr 2005
    Posts
    161
    Thanks
    0
    Thanked 0 Times in 0 Posts
    glenngv, I always thought match("value") returns a string and not an array like match(/value/g). Thanks for the info.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Probably because when you alert the returned array, it returns the string representation of the array. The default toString method of the Array object is a string of comma-delimited list of the elements. Since there's only one element if you use a string as parameter and not a regexp, it seems the returned value is a string because there's no comma in it.

  • #6
    Regular Coder
    Join Date
    Nov 2004
    Location
    near the only active volcano in the continental US
    Posts
    409
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Thanks again, glenn.

    If you've been following my project at all (or even if you haven't), I've been working on a Celestial Positions Calculator. I finally got all the formulas working right and am now looking to get it web ready. At first I was going to go with some basic validation and use alert boxes when values weren't within the proper parameters. Then I happened to be browsing through a few sites like Coding Forums and Java Script Kit and got to thinking about combining validation with the enable/disable feature that can be attached to Form Buttons just to give the whole calculator a more dynamic and intelligent feel.

    Since I am including instructions on how to use the calculator, I am hoping that using these features will accomplish what I want. Still got some work to do since I want to be able to display the results not only in the form fields, but as a table in a second window as well. I think I am getting pretty close now. A few tweaks here and there and I should have it up and running soon.

    You've been mega-helpful once again.

    GW



  •  

    Posting Permissions

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