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

like operator?

willa666
willa666 asked
on
Medium Priority
3,125 Views
Last Modified: 2010-08-05
Is their such a thing as a like in javascript? I have a string and i need to find out if it is like another string.
Comment
Watch Question

Commented:
Try this

<script>
var str1, str2, str3;

str1 = "Hello World";
str2 = "Hello";
str3 = "Not in";

alert(str1.indexOf(str2));
alert(str1.indexOf(str3));
</script>

Commented:
indexOf will give you -1 like in the situation above where str3 is not in str1
indexOf will give you >= 0 (position start from 0) where str2 is first found in str1.

Author

Commented:
coolio!

thats works a treat! what if i want the end to match?

Commented:
Don't understand

Author

Commented:
well instead of it getting the indexof a string it looks for the end of the string to match.

u c

Commented:
indexOf will find it anywhere.

Looking for hello in all of these:

<script>
var str1, str2, str3;

str1 = "Hello";
str2 = "Hello World";
str3 = "Not in here, it isn't";
str4 = "Well, you didn't even say Hello when I saw you yesterday";

alert(str2.indexOf(str1));
alert(str3.indexOf(str1));
alert(str4.indexOf(str1));
</script>

These will return 0, -1, 26 .  Any value>=0 indicates a match.

You should probably turn all strings to lowercase before comparing, because you wouldn't find a match for "Hello" in "You say goodbye and I say hello." because of the difference in capitalization.

Peace and joy to you.
mvan
Commented:
This shows how to do the lowercase properly...  Peace and joy.  mvan

<script>
var str1, str2, str3, str4, str5;

str1 = "Hello";
str2 = "Hello World";
str3 = "Not in here, it isn't";
str4 = "Well, you didn't even say Hello when I saw you yesterday";
str5 = "You say goodbye and I say hello";

document.write(str1+((str2.indexOf(str1)>-1)?' is':' is NOT')+' like: "'+str2+'"<br>');
document.write(str1+((str3.indexOf(str1)>-1)?' is':' is NOT')+' like: "'+str3+'"<br>');
document.write(str1+((str4.indexOf(str1)>-1)?' is':' is NOT')+' like: "'+str4+'"<br>');
document.write('<br>');
document.write(str1+((str5.indexOf(str1)>-1)?' is':' is NOT')+' like: "'+str5+'"<br>');
document.write('<br>');
document.write(' * However, converting all to lowercase for the comparison:<br>');
document.write(str1+((str5.toLowerCase().indexOf(str1.toLowerCase())>-1)?' is':' is NOT')+' like: "'+str5+'"<br>');
document.write('<br>');
document.write(' * test is:  (lookInStr.toLowerCase().indexOf(findMeStr.toLowerCase())>-1)<br>');
document.write('             you supply var lookInStr and var findMeStr<br>');
</script>

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

Ask the Experts
You can also use regular expressions to test strings

var str = "Hello world!";
// test if contains "Ello" case sensitive
var rexp1 = /ello/g;
rexp1.test(str);  // returns false
// test if contains "Ello" not case sensitive
var rexp2 = /ello/ig;
rexp2.test(str); // returns true
// test if ends with "world"
var rexp3 = /world$/;
rexp3.test(str);

Some good references about regular expressions:
http://www.devguru.com/Technologies/ecmascript/quickref/regexp.html
http://www.devguru.com/Technologies/ecmascript/quickref/regexp_special_characters.html
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.