search
The search() method searches for a match with a substring or a regular expression in a string and returns the index of the first occurrence found. If no match is found, it returns undefined.
Interactive example
Syntax
Classic signature
typescript
function search<
GenericInput extends string
>(
input: GenericInput,
pattern: string | RegExp
): number | undefined;Curried signature
typescript
function search<
GenericInput extends string
>(
pattern: string | RegExp
): (input: GenericInput) => number | undefined;Parameters
input: The string in which to perform the search.pattern: The search pattern, which can be a string or a regular expression.
Return value
The index of the first occurrence of the pattern in the string, or undefined if no match is found.
See also
- indexOf: Returns the index of the first occurrence of a substring.
- lastIndexOf: Returns the index of the last occurrence of a substring.
- includes: Checks whether a string contains a substring.
