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:
|
||||
Here is a list of current possibilities: 1. indexOf - (see bottom) 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. Outdated answer:
|
|||||||||||||||||||||
|
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:
|
|||||||||||||||||||||
|
The problem with your code is that JavaScript is case sensitive. Your method call
should actually be
Try fixing it and see if that helps:
|
|||||
|
|
|||
|
Update for 2015:
Note you may need to load
|
|||||||||||||||||||||
|
You could use the JavaScript Syntax is: It returns the position of the match, or -1 if no match is found. See examples there: jsref_search You don't need a complicated regular expression syntax. If you are not familiar with them a simple |
|||||||||||||||||||||
|
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. |
|||||||||||||
|
If you were looking for an alternative to write the ugly -1 check, you prepend a ~ tilde instead.
More details here |
|||||||||||||
|
This piece of code should work well:
|
|||||||||
|
In ES5
In ES6 there are three new methods:
|
|||||||||
|
A common way to write a
The bitwise negation operator ( The double boolean negation operators are used to cast the number into a boolean. |
|||||||||||||||||||||
|
|
|||||||||||||||||
|
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. |
|||||||||||||||||
|
You were looking for
|
||||
|
This just worked for me. It selects for strings that do not contain the term "Deleted:"
|
||||
|
You need to call indexOf with a capital "O" as mentioned. It should also be noted, that in JavaScript class is a reserved word, you need to use className to get this data attribute. The reason it's probably failing is because it's returning a null value. You can do the following to get your class value...
|
|||||
|
Use a regular expression: |
|||||||||||||||||||||
|
Another option of doing this is: You can use the match function, that is, something like:
|
|||||
|
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 . |
|||||
|
Since the question is pretty popular, I thought I could add a little modern flavor to the code.
BTW, the correct answer is misspelling |
||||
|
Simple workaround
you can use in the following way
|
|||||
|
Example
|
||||
|
ES6 contains https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes |
|||||||||
|
|
To collect some kind of valid solutions:
|
|||||||||
|
Since there is a complaint about using the prototype, and since using
That is the compromise I ended up going for. |
||||
|
JavaScript code to use the
In the given code the |
|||||||||||||
|
JavaScript
jQuery
|
||||
|
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. |
||||
|
Use the inbuilt and simplest one i.e
|
||||
|
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