Skip to main content

jQuery String Contains Functions

By Sam Deering



😳 Ever felt a little embarassed sharing your JavaScript? Let's fix it.

Finding whether a string contains another string can be achieved not using jQuery but plain ole JavaScript!

Here is how you do it.

if (str.indexOf("Yes") >= 0)

Note that this is case-sensitive. If you want a case-insensitive search, you can write

if (str.toLowerCase().indexOf("yes") >= 0)
if (/yes/i.test(str))
//You could use search or match for this. 'Yes' )

Another example to check if a string contains another string.

if (v.indexOf("http:") == -1) 
  //string doesn't contain http

This will return the position of the match, or -1 if it isn’t found.

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

New books out now!

💁‍♀️ Fun Fact: Python was ranked #2 on a recent highest paid coders list. *

🤓 Ok. When did a code editor from Microsoft become kinda cool!?