TypeScript String search() Method Last Updated : 12 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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.Syntaxstring.search(regexp);ParameterThis 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() MethodExample 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. TypeScript let str: string = "Hello, welcome to TypeScript!"; let index: number = str.search(/welcome/); if (index !== -1) { console.log("Found at index:", index); } else { console.log("Not Found"); } Output: Found at index: 7Example 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. TypeScript let str: string = "Hello, welcome to TypeScript!"; let index: number = str.search(/world/); if (index !== -1) { console.log("Found at index:", index); } else { console.log("Not Found"); } Output:Not Found Create Quiz Comment S shubhamsingh10 Follow 0 Improve S shubhamsingh10 Follow 0 Improve Article Tags : TypeScript Explore TypeScript BasicsIntroduction to TypeScript3 min readDifference between TypeScript and JavaScript4 min readHow to install TypeScript ?3 min readHello World in TypeScript2 min readHow to execute TypeScript file using command line?2 min readVariables in TypeScript6 min readWhat are the different keywords to declare variables in TypeScript ?4 min readIdentifiers and Keywords in TypeScript2 min readTypeScript primitive typesData types in TypeScript3 min readTypeScript Numbers4 min readExplain the concept of null and its uses in TypeScript3 min readTypeScript Object typesTypeScript class4 min readHow enums works in TypeScript ?4 min readTypeScript Tuples4 min readTypeScript other typesWhat is any type, and when to use it in TypeScript ?3 min readWhat is an unknown type and when to use it in TypeScript ?3 min readExplain the purpose of never type in TypeScript3 min readTypeScript combining typesTypeScript Union3 min readTypeScript AssertionsExplain Type assertions in TypeScript3 min readTypeScript FunctionsHow to write a function in Typescript ?4 min readHow to achieve function overloading in TypeScript ?2 min readExplain the arrow function syntax in TypeScript2 min readTypeScript toPrecision() Function1 min readTypeScript toFixed() Function2 min readTypeScript toLocaleString() Function2 min readTypeScript toString()1 min readTypeScript interfaces and aliasesWhat are TypeScript Interfaces?4 min readWhat are type aliases and how to create it in Typescript ?3 min readTypeScript classesHow to Extend an Interface from a class in TypeScript ?2 min readHow to Create an Object in TypeScript?4 min readHow to use getters/setters in TypeScript ?5 min readTypeScript Inheritance3 min readWhen to use interfaces and when to use classes in TypeScript ?4 min readGenerics Interface in typescript5 min readHow to use property decorators in TypeScript ?4 min readTypeScript modulesWhat are the Modules in Typescript ?4 min readHow to import a module in Typescript ?5 min read Like