Whenever you use match() on a string, the method does the following:
- It creates a new array.
- It populates the newly created array with the patterns that match the method’s regular expression argument.
Syntax of the match()
Method
match()
accepts a regular expression argument. Here is the syntax:
Example 1: Match the First day
Pattern
Try it on StackBlitz
CodeSweetly adsExpress Your Love for Coding!
Explore CodeSweetly's Shop for an array of stylish products to enhance your coding experience.
Shop now
Example 2: Match the First Case-Insensitive day
Pattern
Try it on StackBlitz
Example 3: Do a Global Case-Insensitive Match of day
Try it on StackBlitz
Important Stuff to Know about the match()
Method
Whenever you use a global flag (g
) with match()
’s RegExp argument, the computer will ignore any capturing group in the regular expression.
For instance, the snippet below matched the first (\w+)d(a)y
pattern while also returning the matched capture groups’ text: 'Tues'
and 'a'
.
However, if you add a global flag to the regular expression, the computer will ignore the capture groups.
Here’s an example: