JavaScript
Article
By Sam Deering

jQuery String Contains Functions

By Sam Deering
Last chance to win! You'll get a... FREE 6-Month Subscription to SitePoint Premium Plus you'll go in the draw to WIN a new Macbook SitePoint 2017 Survey Yes, let's Do this It only takes 5 min

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

--ADVERTISEMENT--

if (str.toLowerCase().indexOf("yes") >= 0)
//OR
if (/yes/i.test(str))
//OR
//You could use search or match for this.
str.search( '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.

Login or Create Account to Comment
Login Create Account
Recommended
Sponsors
Get the most important and interesting stories in tech. Straight to your inbox, daily.
Is it good?Is it good?