C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript String search() MethodThe JavaScript string search() method is used to search the regular expression in the given string. This method returns -1, if match is not found. SyntaxThe search() method is represented by the following syntax: string.search(regexp) Parameterregexp - It represents the regular expression which is to be searched. ReturnThe position of searched character. JavaScript String search() Method ExampleLet's see some examples of search() method. Example 1Let's see a simple example to search for a string. <script> var str="JavaScript is a scripting language. Scripting languages are often interpreted"; document.writeln(str.search("scripting")); </script> Output: 16 Example 2In this example, we will see that search() method is case-sensitive. <script> var str="JavaScript is a scripting language. Scripting languages are often interpreted"; document.writeln(str.search(/Scripting/)); </script> Output: 36 Example 3We can ignore case-sensitive behaviour of search() method by using ignore flag. Let's understand with the help of example: <script> var str="JavaScript is a scripting language. Scripting languages are often interpreted"; document.writeln(str.search(/Scripting/i)); </script> Output: 16 Example 4In this example, we will search a regular expression which is not present in the given string. <script> var str="JavaScript is a scripting language. Scripting languages are often interpreted"; document.writeln(str.search(/TheDeveloperBlog/)); </script> Output: -1
Next TopicJavaScript String
|