Gossamer Forum
Home : General : Internet Technologies :

Javascript question about "indexOf()" function!

Quote Reply
Javascript question about "indexOf()" function!
Hello !

I have a simple Javascript code like this

Code:


<script language=javascript>

var data ="Honda/Toyota/Lexus/MBez/Ford";

var check ="Honda";

if (data.indexOf(check) ==-1) {

return true;

}

else{

alert(check + " was used");

return false;

}

</script>


The problem is : If I set var check ="Hon"; I still get the Message Alert "Hon was used" (Honda was used - not Hon)

Can you please help me to solve this sistuation !

Thanks

N.A


Quote Reply
Re: [newage24] Javascript question about "indexOf()" function! In reply to
I've moved this thread, it wasn't perl related.
Quote Reply
Re: [newage24] Javascript question about "indexOf()" function! In reply to
Hello newage24,

The indexof function is working correctly finding Hon in Honda.

But I suppose that is not what you want.

You would be better using a regular expression to find a whole word only match.

See : http://www.juicystudio.com/...vascript/regexp.html

for explanations and a few examples of how to do this.

See also : http://developer.netscape.com/...e/angus_strings.html

for " Example 1 " which may be close to your desired result.

Hope this helps.

kode

Last edited by:

kode: Oct 15, 2002, 9:19 AM
Quote Reply
Re: [newage24] Javascript question about "indexOf()" function! In reply to
Hello Newage24,

I had a thought on how you might get your indexOf working.

See attached honda2.html [ killed the form action for testing purposes ]

I added a / character at the beginning and the end of your data variable, to use as a word boundary.

-- case of a word is a problem still as honda will not match, but Honda will.

Hope this helps.

kode.

Last edited by:

kode: Oct 15, 2002, 10:18 AM