How can I 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.
How can I check if one string contains another substring in JavaScript? Usually I would expect a |
||||
|
|||||||||||||||||||||
|
You can easily add a
Note: see the comments below for a valid argument for not using this. My advice: use your own judgement. |
|||||||||||||||||||||
|
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:
|
|||||||||
|
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 |
|||||||||||||||||||||
|
|
|||||||||
|
A
Of course, we will have to wait decades for mainstream support :P (source) |
|||||||||||||
|
This piece of code should work well:
|
|||||||||
|
You can use jQuery's
Check it here: http://api.jquery.com/contains-selector/ |
|||||||||||||||||
|
Syntax
Parameters
A string to be searched for within this string.
The position in this string at which to begin searching for Example
NoteIt is only supported in Firefox from version 18 onwards. |
||||
|
A common way to write a
The bitwise negation operator ( The double boolean negation operators are used to cast the number into a boolean. |
|||||||||||||||||||||
|
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...
|
|||||
|
If you were looking for an alternative to write the ugly -1 check, you prepend a ~ tilde instead.
More details here |
|||||||||
|
Another option of doing this is: You can use the match function, that is, something like:
|
|||||
|
You were looking for
|
|||||
|
Use a regular expression: |
|||||||||||||||||
|
Since the question is pretty popular, I thought I could add a little modern flavor to the code.
BTW, the correct answer is misspelling |
||||
|
Since there is a complaint about using the prototype, and since using
That is the compromise I ended up going for. |
||||
|
Instead of using code snippets found here and there on the web, you can also use a well-tested and documented library like Underscore.string for this. It has an
Here is the description of the library, 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. |
|||||
|
JavaScript code to use the
In the given code the |
|||||||||||||
|
Use the inbuilt and simplest one i.e
|
||||
|
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. |
||||
|
Try this:
Here is an example: http://jsfiddle.net/oliverni/cb8xw/ |
|||
|
|
||||
|
If you don't like the
|
|||||||||
|
This is a function to check if a substring is existing in a string or not:
|
|||||
|
ES6 contains https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/contains |
||||
|
One more Function
|
||||
|
Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?
String
s andArray
s are fundamentally different things and neither is a special kind of the other. – Mark Amery Oct 8 at 19:27