Checking for a Sub-String in Javascript - All Possible Cases

javascript
Published on June 9, 2020

Javascript provides multiple methods that can check whether a sub-string is contained in a string, depending on various possible use-cases.

  • Whether string contains a sub-string
  • Whether string starts with a sub-string
  • Whether string starts with a sub-string, from a given start position
  • Whether string ends with a sub-string
  • Whether string ends with a sub-string, from a given end position
  • Whether string contains a substring (case-insensitive)
  • Whether string contains a sub-string that matches a regular expression

Checking Whether String Contains a Sub-String

The includes() method checks whether the string contains a given sub-string. This method is case-sensitive.

  • If string contains the given sub-string, this returns true
  • Otherwise it returns false
let str = 'Twinkle twinkle little star';

// true
str.includes('star');

// true
str.includes('little star');

// false
str.includes('TWINKLE');

Checking Whether String Starts With a Sub-String

The startsWith() method checks whether the string starts with a given sub-string. This method is case-sensitive.

  • If string starts with the given sub-string, this returns true
  • Otherwise returns false
let str = 'Twinkle twinkle little star';

// true
str.startsWith('Twinkle');

// false
str.startsWith('little');

Checking Whether String Starts With a Sub-String, From a Given Start Position

startsWith() method also accepts a second parameter that can be used to specify the position from where to start searching.

By default this parameter is 0.

let str = 'Twinkle twinkle little star';

// true
str.startsWith('winkle', 1);

// true
str.startsWith('kle', 4);

Checking Whether String Ends With a Sub-String

The endsWith() method checks whether the string ends with a given sub-string. This method is case-sensitive.

  • If string starts with the given sub-string, this returns true
  • Otherwise it returns false
let str = 'Twinkle twinkle little star';

// true
str.endsWith('star');

// true
str.endsWith('little star');

// false
str.endsWith('twinkle');

Checking Whether String Ends With a Sub-String, From a Given End Position

endsWith() also accepts a second parameter to specify the length of the string to consider. By default this is set to the length of the string.

This parameter can be used to check for a sub-string, from a given end position.

let str = 'Twinkle twinkle little star';

// true
// check if endsWith, excluding the last character of string
str.endsWith('sta', str.length-1);

// true
// check if endsWith, excluding the last 5 characters of string
str.endsWith('little', str.length-5);

Checking Whether String Contains a Sub-String (Case-Insensitive)

For case insensitive search, we need to use regular expressions.

The test() method can be used to test the sub-string regular expression against the main string. The sub-string is used in the regular expression with the flag i — denoting case-insensitive search.

  • If the string contains the sub-string, test() returns true
  • Otherwise false
let str = 'Twinkle twinkle little star';

// define new RegExp object
let regEx = new RegExp("LITTLE", "i");

// alternate way to define the regular expression
let regEx = /LITTLE/i;

// true
regEx.test(str);

Checking Whether String Contains a Sub-String That Matches a Regular Expression

As explained in the above case, the test() method can be used to check whether the provided string matches a regular expression.

  • If the string matches the calling regular expression, test() returns true
  • Otherwise false
let str = "Twinkle twinkle little star";

// checking if the string starts with the alphabet "T" or "t"
let regEx = /^t/i;

// true
regEx.test(str);
Comments
Loading Comments