Link to home
Start Free TrialLog in
Avatar of MitchellVII
MitchellVIIFlag for United States of America

asked on

What is the Javascript equivalent of using Like *?

I want to check an ID value in on my form and use the equivalent of "Like *Combo" as you would in VB.  How does one do this in JS?
Avatar of Badotz
Badotz
Flag of United States of America image

str = 'Multi-COMBO';
var pos = str.indexOf('COMBO');

// pos will = 6

var pos = str.indexOf('combo');

// pos will = -1 (not found)

JavaScript is case sensitive, so compare CASE to CASE:

str = 'Multi-COMBO';
var pos = str.toUpperCase().indexOf('MULTI');

// pos will = 0
There are a number of options.
You can use the "indexOf" function to check if the string you are looking for contains the text.  The function returns the index the search string was found in, or a negative one (-1) if no value was found.
Or you can use a regular expression for a more robust form of pattern matching.



//indexOf() function
alert('This is a test'.indexOf('is')); //returns 2
alert('This is a test'.indexOf('foo')); //returns -1
 
//regular expression(using the test function)
alert(/is a/.test('This is a test'));  //returns true;
alert(/is A/.test('This is a test'));  //returns false;
alert(/IS A/i.test('This is a test'));  //returns true, i flag means ignore case;

Open in new window

Avatar of MitchellVII

ASKER

Badotz,

Lol, now I'm totally confused :)

Let me post the code where I wish to use this and maybe you can show me how.  Basically, I want to cycle through all of my Combo controls on my form and change their background to blue using JS, but I only want to change those with the words "*Combo" in their name.

For instance, I want to change 'Language1Combo' to blue, but leave 'Language1' unchanged, even though they are both combo boxes.

function elmLoop(){
var theForm = document.forms[0]

for(i=0; i<theForm.elements.length; i++)
      {
            if(theForm.elements[i].type == "select-one" && theForm.elements[i].name Like "*Combo") << I know this is wrong, but this is what i want to do...
            {
                  theForm.elements[i].style.backgroundColor='#EBEBF5';
            }
      }
}      
ASKER CERTIFIED SOLUTION
Avatar of Badotz
Badotz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So, using:

theForm.elements[i].indexOf('Combo') <> -1

is the same as saying:

theForm.elements[i].name Like "*Combo"?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks guys.  I am reasonably proficient in Visual Basic but it is hard getting used to Javascript.  Not  a programmer, just a business guy trying to tweak his webforms a bit.

All good answers, I'll spread the points around :).
JohnSixkiller: My bad - mixing VBScript with JavaScript always gives me a headache ;-)

Thanks for the correction.
You are welcome, I'm glad to help.
It is hard to keep the differences between Visual Basic, VB Script and Javascript all straight in your head sometimes.
Well, considering I never use VBScript (anymore), it usually isn't so hard.

But I was working on a client's Excel spreadsheet problem, and that uses VBA, so...