We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

What is the Javascript equivalent of using Like *?

MitchellVII
MitchellVII asked
on
Medium Priority
629 Views
Last Modified: 2012-05-06
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?
Comment
Watch Question

Top Expert 2007

Commented:
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

Author

Commented:
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';
            }
      }
}      
Top Expert 2007
Commented:
Try this:


if(theForm.elements[i].type == "select-one" && theForm.elements[i].indexOf('Combo') <> -1)

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

function elmLoop(){
var theForm = document.forms[0]  //you probably want to use document.getElementById if it's available...
 
for(i=0, e=theForm.elements.length; i<e; ++i)
      {
            if(theForm.elements[i].type === 'select-one' && /Combo/.test(theForm.elements[i].name)) //that would test for combo anywhere in the word.  If you want to search for those that end with combo you could use /Combo$/ instead.
            {
                  theForm.elements[i].style.backgroundColor='#EBEBF5';
            }
      }
}      

Open in new window

Author

Commented:
So, using:

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

is the same as saying:

theForm.elements[i].name Like "*Combo"?
There is a little typo in Badotz expression:

theForm.elements[i].indexOf('Combo')  != -1 means that source string does NOT contains "Combo"
So, using:

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

is the same as saying:

theForm.elements[i].name Like "*Combo"?


Yes and no.  It's more like say "Like '*Combo*'", since as it stands, it will match the word Combo ANYWHERE in the string, not just at the end.

Author

Commented:
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 :).
Top Expert 2007

Commented:
JohnSixkiller: My bad - mixing VBScript with JavaScript always gives me a headache ;-)

Thanks for the correction.
You are welcome, I'm glad to help.

Author

Commented:
It is hard to keep the differences between Visual Basic, VB Script and Javascript all straight in your head sometimes.
Top Expert 2007

Commented:
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...
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.