VOOZH about

URL: https://www.geeksforgeeks.org/typescript/typescript-string-search-method/

⇱ TypeScript String search() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

TypeScript String search() Method

Last Updated : 12 Jul, 2024

The search() method is an inbuilt function in TypeScript that is used to search for a match between a regular expression and a string. It returns the index of the first match, or -1 if no match is found.

Syntax

string.search(regexp);

Parameter

This methods accept a single parameter as mentioned above and described below: 

  • regexp: This parameter is a RegExp object.

Return Value:

  • The method returns the index of the regular expression inside the string. If no match is found, it returns -1.

Examples of TypeScript String search() Method

Example 1: Finding a Substring with search()

In this example, we use the search() method to find the position of a substring that matches a regular expression.

Output:

Found at index: 7

Example 2: No Match Found with search()

In this example, we use the search() method to look for a substring that does not exist in the string.

Output:

Not Found 
Comment
Article Tags:
Article Tags:

Explore