View Full Version : Match() Method Clarification

01-23-2006, 06: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?

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

01-23-2006, 07:20 AM
Assuming "value" is in the string:

if you use:


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

if you use:


it returns the first match which will also evaluate to true

try this

var str='abc';



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

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

01-23-2006, 08: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.

01-23-2006, 09: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.