Home / Tips / Programming languages / Scripting languages / JavaScript / How to check if a string contains substring in JavaScript?

How to check if a string contains substring in JavaScript?

JavaScript
 
JavaScript
  • Currently 0 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Rating: 0/5 (0 votes cast)

Thank you for rating!

You have already rated this page, you can only rate it once!

Your rating has been changed, thanks for rating!

Log in or create a user account to rate this page.


To check this you can use String.indexOf(subString), which returns position of the first occurrence of subString or -1, if it wasn't found.

var str= 'test Text';
var subStr = 'test';
if( str.indexOf( subStr ) !== -1 ) alert( 'substring found' );

indexOf is case sensitive, to check for subString regardless of its case sensitivity you can use the following code:

var str= 'test Text';
var subStr = 'TEXT';
if( str.toLowerCase().indexOf( subStr.toLowerCase() ) !== -1 ) alert( 'substring found' );

 

Read also

Post comment

basicuse.net
html5_css
106160485398655174790

Quick navigation

General navigation