like operator?

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.
LVL 1
willa666Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hongjunCommented:
Try this

<script>
var str1, str2, str3;

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

alert(str1.indexOf(str2));
alert(str1.indexOf(str3));
</script>
0
hongjunCommented:
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.
0
willa666Author Commented:
coolio!

thats works a treat! what if i want the end to match?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

hongjunCommented:
Don't understand
0
willa666Author Commented:
well instead of it getting the indexof a string it looks for the end of the string to match.

u c
0
mvan01Commented:
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
0
mvan01Commented:
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>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ppedregalCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.