Searching in Strings

This YouTube video was created by Steve Griffith.

JavaScript includes a few different methods that can be used to search for a string within another string.

indexOf()

The indexOf() method return the index within a string of the first occurrence of the specified substring. The method will return -1 if the substring could not be found.

const phrase = 'Almost before we knew it, we had left the ground.'

// get the index of the string 'we'
console.log(phrase.indexOf('we')) // 14

// get the index of the string 'we' after index 20
console.log(phrase.indexOf('we', 20)) // 26

// get the index of the string 'ship'
console.log(phrase.indexOf('ship')) // -1

Also see the lastIndexOf() method for returning the index of the last occurrence of the specified substring.

includes()

The includes() method determines whether a substring is within another string. The method returns true or false appropriately.

const phrase = 'Almost before we knew it, we had left the ground.'

// check for the string 'we'
console.log(phrase.includes('we')) // true

// check for the string 'ship'
console.log(phrase.includes('ship')) // false

match()

The match() method retrieves the result of matching a string against a regular expression. The method will return and array with any matched patterns.

const quote = 'Beware the Ides of March.'

// Match for uppercase letters
console.log(quote.match(/[A-Z]/g)) // ['B', 'I', 'M']

// Match for word follow by comma
console.log(quote.match(/(\w+,)/g)) // null

test()

The test() method searches for match between a regular expression and the provided string. The method will return true is the a match is found and false if not.

NOTE

The test() method is NOT a string method, but a method of the RegExp object. However, it is considered the best way to confirm if a pattern exists inside a string.

const quote = 'Romeo, Romeo! wherefore art thou Romeo?'

// Search for a character that is not a letter or whitespace
console.log(/[^\s\w]/g.test(quote)) // true

// Search for a period (.)
console.log(/[.]/g.test(quote)) // false

substring()

The substring() method returns part of a string between the provided start index and the provided end index or the end of the string.

NOTE

Do NOT confused the substring() method with the substr() method. While substr() is a valid method, it is considers to be a legacy function and should not be used.

const greeting = 'Hello, World!'

// includes the first index but excludes the second
console.log(greeting.substring(0, 5)) // 'Hello'

// if only one index, goes to the end of the string
console.log(greeting.substring(7)) // 'World!'

// swaps the indexes, if the first is larger
console.log(greeting.substring(12, 7)) // 'World'

// negative indexes are treated as 0
console.log(greeting.substring(-6)) // 'Hello, World!'

// negative indexes are treated as 0
console.log(greeting.substring(-6, -1)) // ''

startsWith()

The startsWith() method determines whether a string begins with the specified substring. The method returns true or false appropriately.

const phrase = 'Almost before we knew it, we had left the ground.'

// check if the string 'Almost' is at the beginning
console.log(phrase.startsWith('Almost')) // true

// check if the string 'we' is at the beginning
console.log(phrase.startsWith('we')) // false

Also see the endsWith() method for determining whether a string ends with the specified substring.