0

From this famous high-voted question I've find an effective way to check if a string contains another string or not with String.indexOf.

var abcde = "abcdefg";
var abc = "abc";
alert(abc.indexOf(abcde) != -1);//return true

But when I try to do like this:

var url = "https://stackoverflow.com/questions/1789945/method-like-string-contains-in-javascript";
var s = "stackoverflow";
alert(s.indexOf(url) != -1); //**return false,but I think it 'should' return true!**

I'm curious that why a more complex which contains symbol or slash / seems to be failed. Or did I miss something?

1 Answer 1

5

The string you search is the parameter, not the receiver.

Change

alert(s.indexOf(url) != -1); 

to

alert(url.indexOf(s) != -1); 

(and you should also use console.log instead of alert, for your own comfort)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.