C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript String match() MethodThe JavaScript string match() method is used to match the string against a regular expression. We can use global search modifier with match() method to get all the match elements otherwise the method return only first match. SyntaxThe match() method is represented by the following syntax: string.match(regexp) Parameterregexp - It represents the regular expression which is to be searched. ReturnThe matched regular expression. JavaScript String match() Method ExampleLet's see some simple examples of match() method. Example 1Let's see a simple example to search for a match. <script> var str="TheDeveloperBlog"; document.writeln(str.match("Java")); </script> Output: Java Example 2In this example, we will search for a regular expression using global flag. <script> var str="TheDeveloperBlog"; document.writeln(str.match(/Java/g)); </script> Output: Java Example 3Let's see one more example to search for a regular expression using global flag. As match() method is case-sensitive, it return null in this case. <script> var str="TheDeveloperBlog"; document.writeln(str.match(/java/g)); </script> Output: null Example 4We can ignore case-sensitive behaviour of match() method by using ignore flag. Let's understand with the help of example: <script> var str="TheDeveloperBlog"; document.writeln(str.match(/java/gi)); </script> Output: Java Example 5Here, we will print the array of matched elements. <script> var str="TheDeveloperBlog"; document.writeln(str.match(/[a-p]/g)); </script> Output: a,a,p,o,i,n Example 6Let's see the same example without using global search. <script> var str="TheDeveloperBlog"; document.writeln(str.match(/[a-p]/));//return the first match </script> Output: a
Next TopicJavaScript String
|