Like this:
if (str.indexOf("Yes") >= 0)
...or you can use the tilde operator:
if (~str.indexOf("Yes"))
This works because indexOf() returns -1 if the string wasn't found at all.
Note that this is case-sensitive.
If you want a case-insensitive search, you can write
if (str.toLowerCase().indexOf("yes") >= 0)
Or:
if (/yes/i.test(str))
The latter is a regular expression or regex.
Regex breakdown:
/indicates this is a regexyesmeans that the regex will find those exact characters in that exact order/ends the regexisets the regex as case-insensitive.test(str)determines if the regular expression matchesstrTo sum it up, it means it will see if it can find the lettersy,e, andsin that exact order, case-insensitively, in the variablestr
Top answer 1 of 12
2391
Like this:
if (str.indexOf("Yes") >= 0)
...or you can use the tilde operator:
if (~str.indexOf("Yes"))
This works because indexOf() returns -1 if the string wasn't found at all.
Note that this is case-sensitive.
If you want a case-insensitive search, you can write
if (str.toLowerCase().indexOf("yes") >= 0)
Or:
if (/yes/i.test(str))
The latter is a regular expression or regex.
Regex breakdown:
/indicates this is a regexyesmeans that the regex will find those exact characters in that exact order/ends the regexisets the regex as case-insensitive.test(str)determines if the regular expression matchesstrTo sum it up, it means it will see if it can find the lettersy,e, andsin that exact order, case-insensitively, in the variablestr
2 of 12
138
You could use search or match for this.
str.search( 'Yes' )
will return the position of the match, or -1 if it isn't found.
SitePoint
sitepoint.com › blog › javascript › jquery string contains functions
jQuery String Contains Functions — SitePoint
February 13, 2024 - Finding whether a string contains another string can be achieved not using jQuery but plain ole JavaScript! Using the str.indexOf() function and str.search().
Select All Elements Whose Id Contains String with jQuery ...
04:42
JavaScript String Contains: How to check a string exists in another ...
04:09
check if string contains specific text using jQuery - YouTube
- YouTube
02:22
MVC - jQuery - Check if strings contains specified text (substring) ...
jQuery
api.jquery.com › contains-selector
:contains() Selector | jQuery API Documentation
Categories: Selectors > Content Filter | Selectors > jQuery Extensions · Description: Select all elements that contain the specified text. text: A string of text to look for.
W3Schools
w3schools.com › jsref › jsref_includes.asp
W3Schools.com
The includes() method returns true if a string contains a specified string.
jQuery
api.jquery.com › jQuery.contains
jQuery.contains() | jQuery API Documentation
The $.contains() method returns ... nodes are supported; if the second argument is a text or comment node, $.contains() will return false. Note: The first argument must be a DOM element, not a jQuery object or plain JavaScript object....
W3Schools
w3schools.com › jquery › sel_contains.asp
jQuery :contains() Selector
jQuery Overview jQuery Selectors jQuery Events jQuery Effects jQuery HTML/CSS jQuery Traversing jQuery AJAX jQuery Misc jQuery Properties ... The :contains() selector selects elements containing the specified string.
Edureka Community
edureka.co › home › community › categories › web development › jquery › jquery string contains check
JQuery string contains check | Edureka Community
June 15, 2022 - I need to check whether a string contains another string or not? var str1 = "ABCDEFGHIJKLMNOP"; var ... do I use to find out if str1 contains str2?
Google
developers.google.com › identity › protocols › oauth2 › javascript-implicit-flow
OAuth 2.0 for Client-side Web Applications | Google for Developers
August 7, 2026 - If the user does not approve the request, the response contains an error message. The access token or error message is returned on the hash fragment of the redirect URI, as shown in the following examples: ... In addition to the access_token parameter, the fragment string also contains the token_type parameter, which is always set to Bearer, and the expires_in parameter, which specifies the lifetime of the token, in seconds.
jQuery
api.jquery.com › jQuery
jQuery() | jQuery API Documentation
Description: Accepts a string containing a CSS selector which is then used to match a set of elements. ... A DOM element to wrap in a jQuery object.
Jquery
forum.jquery.com › portal › en › community › topic › if-string-contains-string
If string contains string
We cannot provide a description for this page right now
Squash
squash.io › how-to-check-if-a-string-contains-a-substring-in-javascript
How To Check If A String Contains A Substring In Javascript
August 25, 2023 - One of the simplest and most straightforward ways to check if a string contains a substring in JavaScript is by using the includes() method.
jQuery Mobile
demos.jquerymobile.com › 1.0a1 › experiments › api-viewer › docs › jQuery.contains › index.html
jQuery.contains()
Description: Check to see if a DOM node is within another DOM node · containerThe DOM element that may contain the other element