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?
Join them; it only takes a minute:
|
||||
Edit: Since the original answer from 2009 is outdated, here a list of other possibilities from this thread: 1. indexOf - (see above) 2. (ES6) includes - go to answer, or this answer
3. search - go to answer
4. lodash includes - go to answer
5. RegExp - go to answer
6. Match - go to answer
Performance tests (http://jsben.ch/#/RVYk7) are showing that indexOf might be the best choice, if it comes to a point where speed matters. |
|||||||||||||||||||||
|
|
One more function,
|
|||||
|
A common way to write a
The bitwise negation operator ( The double boolean negation operators are used to cast the number into a boolean. |
|||||||||||||||||||||
|
With ECMAScript 2015, we can use
|
|||||
|
Syntax
Parameters
A string to be searched for within this string.
The position in this string at which to begin searching for Example
NoteThis may require ES6 shim in older browsers. |
|||||||||
|
In ES5
In ES6 there are three new methods:
|
|||||||||
|
You can easily add a
Note: see the comments below for a valid argument for not using this. My advice: use your own judgement. Alternatively:
|
|||||||||||||||||||||
|
There are multiple ways to do this. But most of the time, you will be fine using the
|
||||
|
ES6 contains https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes |
|||||||||
|
Example
|
||||
|
You were looking for
|
||||
|
You can also do something like this
|
|||||
|
You can use
|
||||
|
Instead of using code snippets found here and there on the web, you can also use a well-tested and documented library. Two Options I would recommend: 1st option: Use Lodash: It has an
Lodash is the most popular javascript library dependency for npm and has loads of handy javascript utility methods. So for many projects you would want this anyway ;-) 2nd option: Or use Underscore.string: It has an
Here is the description of Underscore.string, it just adds 9kb but gives you all the advantages a well-tested and documented library has over copy'n'paste code snippets:
Note well, Underscore.string is influenced by Underscore.js but can be used without it. Last not Least: With JavaScript version ES6 comes an built-in
Most modern browsers already support it, have an eye on the ES6 compatibility table. |
|||||||||||||||||
|
If you were looking for an alternative to write the ugly -1 check, you prepend a ~ tilde instead.
More details here |
|||||||||||||
|
Try this:
Here is an example: jsfiddle |
||||
|
|
|||||||||||||||||
|
I know that best way is I suggest another way(
|
||||
|
Simple workaround
you can use in the following way
|
|||||
|
To collect some kind of valid solutions:
|
|||||||||
|
Update for 2015:
Note you may need to load
|
|||||||||||||||||||||
|
JavaScript
jQuery
|
||||
|
There is a sleek and better way to do this and it is using the (BitWise NOT) operator.
The Bitwise Not converts "x" into -(x + 1) so, if the x turns out -1 from indexOf method.then it will be converted into -( -1 + 1) = -0 which is a falsy value . |
|||||
|
This is a function to check if a substring is existing in a string or not:
|
|||||
|
The easyest way is indeed using indexOf. To just check a string
As you wanted the function
Now you can use this ecen shorter method to check if a string contains a special substring:
Here is a JSFiddle as well. |
||||
|
If you don't like the
|
|||||||||
|
Since the question is pretty popular, I thought I could add a little modern flavor to the code.
BTW, the correct answer is misspelling |
||||
|
JavaScript code to use the
In the given code the |
|||||||||||||
|
|
||||
|
protected by Sean Vieira Nov 2 '12 at 12:31
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
r.indexOf(s) !== -1;
fastest than others. hayageek.com/javascript-string-contains – Sherali Turdiyev Oct 1 '15 at 5:37