TypeScript | String search() Method

The search() is an inbuilt function in TypeScript which 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: 

  • 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



filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // 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" ); 
    }
</script>

chevron_right


Output: 

Found

Example 2:

JavaScript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // 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" ); 
    }
</script>

chevron_right


Output:

Not Found

full-stack-img




My Personal Notes arrow_drop_up

Small things always make you to think big

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.