Strings: Searching Strings

Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026

The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.


JavaScript provides several methods to search within strings.

indexOf()

Returns the position of the first occurrence of a substring. Returns -1 if not found:

'JavaScript'.indexOf('Script') //4
'JavaScript'.indexOf('JavaScript') //0
'JavaScript'.indexOf('aSc') //3
'JavaScript'.indexOf('C++') //-1

You can pass a second parameter to set the starting position:

'a nice string'.indexOf('nice') !== -1 //true
'a nice string'.indexOf('nice', 3) !== -1 //false
'a nice string'.indexOf('nice', 2) !== -1 //true

lastIndexOf()

Returns the position of the last occurrence of a substring. Returns -1 if not found:

'JavaScript is a great language. Yes I mean JavaScript'.lastIndexOf('Script') //47
'JavaScript'.lastIndexOf('C++') //-1

includes()

Returns true if the string contains the substring, false otherwise:

'JavaScript'.includes('Script') //true
'JavaScript'.includes('script') //false
'JavaScript'.includes('JavaScript') //true
'JavaScript'.includes('aSc') //true
'JavaScript'.includes('C++') //false

Accepts an optional second parameter to specify where to start searching:

'a nice string'.includes('nice') //true
'a nice string'.includes('nice', 3) //false
'a nice string'.includes('nice', 2) //true

startsWith()

Checks if a string starts with the specified substring:

'testing'.startsWith('test') //true
'going on testing'.startsWith('test') //false

Accepts a second parameter to specify at which character to start checking:

'testing'.startsWith('test', 2) //false
'going on testing'.startsWith('test', 9) //true

endsWith()

Checks if a string ends with the specified substring:

'JavaScript'.endsWith('Script') //true
'JavaScript'.endsWith('script') //false

You can pass a second parameter to treat the string as if it were that many characters long:

'JavaScript'.endsWith('Script', 5) //false
'JavaScript'.endsWith('aS', 5) //true

Returns the position of the first occurrence of a pattern, or -1 if not found. Accepts regular expressions:

'JavaScript'.search('Script') //4
'JavaScript'.search('TypeScript') //-1

With regular expressions:

'JavaScript'.search(/Script/) //4
'JavaScript'.search(/script/i) //4
'JavaScript'.search(/a+v/) //1

match()

Matches a string against a regular expression and returns an array of matches:

'Hi Flavio'.match(/avio/)
// Array [ 'avio' ]

'Test 123123329'.match(/\d+/)
// Array [ "123123329" ]

'hey'.match(/(hey|ho)/)
//Array [ "hey", "hey" ]

'123s'.match(/^(\d{3})(\w+)$/)
//Array [ "123s", "123", "s" ]

With word boundaries:

'I saw a bear'.match(/\bbear/)    //Array ["bear"]
'I saw a beard'.match(/\bbear/)   //Array ["bear"]
'I saw a beard'.match(/\bbear\b/) //null
'cool_bear'.match(/\bbear\b/)     //null

localeCompare()

Compares a string to another, returning a number that tells if the current string is lower, equal, or greater than the string passed as argument:

'a'.localeCompare('à') //-1
'a'.localeCompare('à', 'it-IT') //-1

The most common use case is for ordering arrays:

['a', 'b', 'c', 'd'].sort((a, b) => a.localeCompare(b))

This is better than using comparison operators because localeCompare() works correctly with international alphabets.

Lessons in this unit:

0: Introduction
1: String Basics
2: Accessing Characters
3: ▶︎ Searching Strings
4: Extracting Substrings
5: Transforming Strings
6: Modifying Strings
7: Trimming and Padding
8: String Recipes
9: Unicode and UTF-8
10: Printable ASCII characters list
11: Non-printable ASCII characters list