Please enable Javascript to correctly display the contents on Dot Net Tricks!
Become an Expert in C#, ASP.NET MVC, JavaScript, AngularJS, NodeJS, Ionic and Android
by Joining our Training Programs and Take Your Career to the Next Level! To know more make a call on +91 98 71 749695

How to find a string within a jQuery or javascript string

Posted By : Shailendra Chauhan, 13 Apr 2013
Total Views : 32,147   
Keywords : jquery string contains function,find a substring in a string in javascript or jquery,finding sub string in jquery code form dynamic string

Sometimes, you required to find a the existence of a small string with in a string. This article will demonstarte, how could you do by using plain JavaScript.

First Method

//returns -1 if doesn't contains
if (str.indexOf("Hello") >= 0) 
 //string contains Hello
//For your are not sure about the case of the string
//returns -1 if doesn't contains
if (str.toLowerCase().indexOf("hello") >= 0)
 //string contains hello
This indexOf() method will return the position of the match, or -1 if it isn’t found.

Second Method

//returns false if doesn't contains
if (/hello/i.test(str))
 //string contain Hello
What do you think?

I hope you will enjoy the tips while playing with JavaScript. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Further Reading
About the Author
Hey! I'm Shailendra Chauhan full-time author, consultant & trainer. I have more than 6 years of hand over Microsoft .NET technologies and other web technologies like JavaScript, AngularJS, NodeJS etc. I am an entrepreneur, the founder & chief editor of and I am author of most popular e-books for technical Interview on ASP.NET MVC Interview Questions and Answers & AngularJS Interview Questions and Answers & LINQ Interview Questions and Answers.
I have delivered 100+ training sessions to professional world-wide over Microsoft .NET technologies such C#, ASP.NET MVC, WCF, Entity Framework and other mobile technologies such Ionic, PhoneGap, Cordova. Read more...
Free Interview Books
Browse By Category
Like us on Facebook