Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

TypeScript | String search() Method

  • Last Updated : 17 Feb, 2021

The search() is an inbuilt function in TypeScript that is used to search for a match between a regular expression and this String object. 
Syntax:

string.search(regexp);

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

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • regexp: This parameter is a RegExp object.

Return Value: This method returns the index of the regular expression inside the string. Otherwise, it returns -1. 
Below example illustrate the  String search() method in TypeScriptJS:

Example 1:



Javascript




// This is TypeScript
// Original strings
var str = "Geeksforgeeks - Best Platform";
 
var re = /Best/gi;
 
// use of String search() Method
if (str.search(re) == -1 ) {
  console.log("Not Found" );
} else {
  console.log("Found" );
}

Output: 

Found

Example 2:

JavaScript




// This is TypeScript
// Original strings
var str = "Geeksforgeeks - Best Platform";
 
var re = /geeek/gi;
 
// use of String search() Method
if (str.search(re) == -1 ) {
    console.log("Not Found" );
} else {
    console.log("Found" );
}

Output:

Not Found 
My Personal Notes arrow_drop_up
Recommended Articles
Page :