TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

JavaScript String match() Method

JavaScript string match() Method with example, by string literal, by string object (using new keyword), javascript string methods, charAt(index) method, concat(str) method, indexOf(str) method, lastIndexOf(str) method, lowerCase() method, upperCase() method, slice(beginIndex, endIndex) method, trim() method, toLowerCase(), slice(), concat(), charCodeAt() method etc.

<< Back to JAVASCRIPT

JavaScript String match() Method

The 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.

Syntax

The match() method is represented by the following syntax:

string.match(regexp)

Parameter

regexp - It represents the regular expression which is to be searched.

Return

The matched regular expression.

JavaScript String match() Method Example

Let's see some simple examples of match() method.

Example 1

Let's see a simple example to search for a match.

<script>
var str="TheDeveloperBlog";
document.writeln(str.match("Java"));
</script>
Test it Now

Output:

Java

Example 2

In this example, we will search for a regular expression using global flag.

<script>
var str="TheDeveloperBlog";
document.writeln(str.match(/Java/g));
</script>
Test it Now

Output:

Java

Example 3

Let'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>
Test it Now

Output:

null

Example 4

We 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>
Test it Now

Output:

Java

Example 5

Here, we will print the array of matched elements.

<script>
var str="TheDeveloperBlog";
document.writeln(str.match(/[a-p]/g));
</script>
Test it Now

Output:

a,a,p,o,i,n

Example 6

Let's see the same example without using global search.

<script>
var str="TheDeveloperBlog";
document.writeln(str.match(/[a-p]/));//return the first match
</script>
Test it Now

Output:

a

Next TopicJavaScript String




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf