...

View Full Version : Match() Method Clarification



greasonwolfe
01-23-2006, 05:05 AM
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?

glenngv
01-23-2006, 06:12 AM
If a match is found, it returns an array of the matched strings, otherwise, it returns null.

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

var arr = stringOBJ.match(/value/i); //i means ignore caseSince 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.

gph
01-23-2006, 06:20 AM
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');

gph
01-23-2006, 06:24 AM
glenngv, I always thought match("value") returns a string and not an array like match(/value/g). Thanks for the info.

glenngv
01-23-2006, 07:07 AM
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.

greasonwolfe
01-23-2006, 08:35 AM
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

:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum