Version: v0.8.1 - Beta.  We welcome contributors & feedback.

String.indexOf

indexOf($matchString, $options={}) -> number

Description

Find the index position of $matchString.

If the substring does not exist, it will return 0.

Note: To simply check if the substring exists, see contains().

'abc xyz'.indexOf('abc')
//= 1

'abc xyz'.indexOf('xyz')
//= 5

'abc xyz'.indexOf('jkl')
//= 0

Options

Option Value Description
ignoreCase true/false Match upper and lowercase versions of each character.
startIndex number Start at index position (default: 1)
'abc xyz'.indexOf('XYZ', -ignoreCase)
//= 5

'abc xyz'.indexOf('XYZ', { ignoreCase, startIndex: 3 })
//= 5

Regex Patterns

The $matchString argument can also be a Regex.

// \d+ = one or more consecutive digits
'ABC 123'.indexOf(rx'\d+')
//= 5

// i = ignore case
'abc def XYZ'.indexOf(rx'xyz'i)
//= 9

See Also