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 |
|||||||||
|
indexOf returns the position of the string in the other string. If not found, it will return -1:
|
|||||||||||||||||||||
|
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 |
|||||||||||||
|
|
|||
|
You can use jQuery's
Check it here: http://api.jquery.com/contains-selector/ |
|||||||||||||||||
|
This piece of code should work well:
|
|||||||||
|
A
Of course, we will have to wait decades for mainstream support :P (source) |
|||||||||
|
This just worked for me. It selects for strings that do not contain the term "Deleted:"
|
||||
|
A common way to write a
The bitwise negation operator ( The double boolean negation operators are used to cast the number into a boolean. |
|||||||||||||||||||||
|
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...
|
|||||
|
Another option of doing this is: You can use the match function, that is, something like:
|
||||
|
Your code works, however you have misspelled the API function. As a result of the misspelling, you were getting the error "Uncaught TypeError: Object title has no method 'indexof'" which was preventing the execution of the rest of your script. Many functions in javascript use the "camel case" naming convention. This dictates that the first letter be lower case, and the first letter of every other word be capitalized. Thus, by convention, you were looking for
striked out content was in response to a later edit to the question related meta post |
||||
|
Use regular expression |
|||||
|
|
||||
|
JavaScript code to use contain method in an array
In the given code the contain method determines whether the specified element is present in the array or not. If the specified element is present in the array, it returns true otherwise it returns false. |
|||
|
Since there is a complaint about using the prototype, and since using indexOf makes your code less readable, and since regexp is overkill:
That is the compromise I ended up going for. |
|||
|
Syntax
Parameters
A string to be searched for within this string.
The position in this string at which to begin searching for Example
NoteOnly supported in Firefox from version 19. |
||||
|
Since the question is pretty popular, I thought I could add a little modern flavor to the code.
Btw the correct answer is misspelling |
||||
|
Try this:
Here is an example: http://jsfiddle.net/oliverni/cb8xw/ |
|||
|
this is function to check a substring is exists in a string or not
|
|||||
|
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. |
|||||
|
This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.