How to check if one string contains another substring in JavaScript?

Usually, I would expect a String.contains() method, but there doesn’t seem to be one. What is the reasonable way to check for this?

1 Like

Well there should be includes

http://www.w3schools.com/jsref/jsref_includes.asp

Ps, I remove the link in your post since it has nothing to do with your question. But I do not want to consider your complete post a spam.

2 Likes

The suggested string.includes is only compatible with very very recent JS engines and will break in a load of places.

Use instead:

"my string is here".indexOf("here") >= 0

1 Like

I do not use JS by myself, I just asked google for an answer :wink:

1 Like

This would be the safest bet if you aren’t using a transpiler like Babel.

2 Likes