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 regexyes
means that the regex will find those exact characters in that exact order/
ends the regexi
sets the regex as case-insensitive.test(str)
determines if the regular expression matchesstr
To sum it up, it means it will see if it can find the lettersy
,e
, ands
in that exact order, case-insensitively, in the variablestr
Related Posts:
- How do I check if string contains substring?
- How to check whether a string contains a substring in JavaScript?
- How can I get the last character in a string?
- How can I get the last character in a string?
- Escaping HTML strings with jQuery
- Does Python have a string ‘contains’ substring method?
- How to change CSS using jQuery?
- How to properly use jsPDF library
- Uncaught ReferenceError: $ is not defined?
- jQuery document.createElement equivalent?
- How do I check if a string contains a specific word?
- Toggle show/hide on click with jQuery
- Pure JavaScript equivalent of jQuery’s $.ready() – how to call a function when the page/DOM is ready for it [duplicate]
- jQuery .on(‘change’, function() {} not triggering for dynamically created inputs
- How to create a jQuery function (a new jQuery method or plugin)?
- Failed to load resource: the server responded with a status of 500 (Internal Server Error) in Bind function
- Scroll to an element with jQuery
- How do you reverse a string in-place in JavaScript?
- How can I scroll to an element using jQuery?
- jQuery Get Selected Option From Dropdown
- What is the correct way to check for string equality in JavaScript?
- Uncaught TypeError: Cannot read property ‘length’ of undefined
- Jquery : Refresh/Reload the page on clicking a button
- Check if element exists in jQuery
- Checking a Url in Jquery/Javascript
- Uncaught TypeError: $.post is not a function
- When and why to ‘return false’ in JavaScript?
- Export html table data to Excel using JavaScript / JQuery is not working properly in chrome browser
- querySelector vs. getElementById
- How to generate a simple popup using jQuery
- AJAX response error: net::ERR_EMPTY_RESPONSE
- XML Parsing Error: no root element found Location in Console FF
- document.getElementByID is not a function
- Moment js date time comparison
- Understanding how data-dismiss attribute works in Bootstrap
- Add table row in jQuery
- Create table with jQuery – append
- What does ${} (dollar sign and curly braces) mean in a string in Javascript?
- How to create a JQuery Clock / Timer
- When is JavaScript synchronous?
- How to reset a form using jQuery with .reset() method
- Access Control Request Headers, is added to header in AJAX request with jQuery
- Mismatched anonymous define() module
- How to set ‘X-Frame-Options’ on iframe?
- Convert UTC date time to local date time
- explode string in jquery
- Play an audio file using jQuery when a button is clicked
- document.getElementsByClassName().innerHTML always returns “undefined”
- Jquery button click() function is not working
- How to define Global Arrays?
- Why is this jQuery click function not working?
- Disable Chrome strict MIME type checking
- How to uncheck a radio button?
- How can I extract a number from a string in JavaScript?
- Phone mask with jQuery and Masked Input Plugin
- JQuery get all elements by class name
- Attaching ‘onclick’ event to D3 chart background
- How do I check whether a checkbox is checked in jQuery?
- The preferred way of creating a new element with jQuery
- jQuery count child elements
- How do I check if an element is hidden in jQuery?
- Creating a div element in jQuery
- jQuery equivalent of querySelector
- How to remove part of a string?
- Convert normal date to unix timestamp
- jQuery replace one class with another
- Clearing my form inputs after submission
- Changing the image source using jQuery
- Get class list for element with jQuery
- Reload an iframe with jQuery
- Convert array to JSON
- How to disable scrolling temporarily?
- How to manage a redirect request after a jQuery Ajax call
- How do you remove all the options of a select box and then add one option and select it with jQuery?
- Get the Highlighted/Selected text
- Replace all spaces in a string with ‘+’
- How to get the children of the $(this) selector?
- window.onload vs $(document).ready()
- Why is jquery show() not working in example
- How can I use jQuery to run MySQL queries?
- jQuery scrollTop() method not working
- FormData append not working
- Print a div content using Jquery
- Uncaught TypeError: Cannot read property ‘toLowerCase’ of undefined
- How can JavaScript save to a local file?
- How to go to a URL using jQuery?
- How to close a window using jQuery
- JQuery Number Formatting
- How to add jQuery in JS file
- Get query string parameters url values with jQuery / Javascript (querystring)
- jQuery .animate() not working
- Adding JavaScript to a WordPress website
- is_page_template() written in jquery/javascript
- On click load iframe [closed]
- Can’t get rid of JQMIGRATE: Migrate is installed, version 3.3.2. Manually updated all the libralies and site. 5.8.2
- Script only works on wordpress home page & only when Jqurery is aded using
- Error on clicking ‘Screen Options’ tab only at Products-related editor screen(s) and for specific user
- Select parent from child class name then add another class name to the parent
- Uncaught ReferenceError: tippy is not defined
- Error – Uncaught TypeError: e.dispatchEvent is not a function