JavaScript match()

javascript string match

Description:

The String.prototype.match() method retrieves the result of
matching a string against a regular expression.

Syntax:

str.match(regexp)

We’ll use the below common string for all our regex examples

var st = "i am iron man"

Examples:

1. No Capturing groups and No g (global flag)

This regex will return only the first match of the pattern and the index where it occurs in the given string (in this case st)

// Let's define our regex:
// Find a lowercase `a` character in the string, 
// but not globally (since no global flag) given,
// so it just find the 1st occurrence.
// No Capturing groups and No g (global flag)

var reg = /a/
st.match(reg)
// returns: 
["a", index: 2, input: "i am iron man", groups: undefined]

2. No Capturing groups and With a g (global flag)

This will return all the occurrence of the pattern in the string

// Let's define our regex:
// Match an a character in the string, 
// globally, which means, if it occurs multiple times, 
// and return it those number of times.
// No Capturing groups and With a g (global flag)

var reg = /a/g
st.match(reg)
// returns: 
["a", "a"]

3. One Capturing group and No g (global flag)

This regex will return only the first match of the pattern

// Let's define the regex
// Find an `a` character in the string, 
// but not globally (no `g` flag) given,
// which means, just find the 1st occurrence,
// and its related capturing groups and return it.
// One Capturing group and No g (global flag)

var reg = /(a)/
st.match(reg)
// returns: 
["a", "a", index: 2, input: "i am iron man", groups: undefined]

4. One Capturing group and With g (global flag)

This will return all the occurrence of the pattern in the string

// Let's define this regex, 
// Find an `a` character in the string, globally, 
// which means, if it occurs multiple times, 
// return all those occurrences.
// One Capturing group and With g (global flag)

var reg = /(a)/g
st.match(reg)
// returns: 
["a", "a"]

5. One “Named” Capturing group and No g (global flag)

This regex will return only the first match of the pattern,

// Let's define this regex, 
// Match an `a` character in the string,
// but not globally (no `g` flag) given,
// which means, just find the 1st occurrence,
// and since there is a "Named" capturing group 
// store it in the groups property of the returned matches 
// under the name specified by the angle brackets 
// These angles ('<' and '>') are required for group name.
// In this case keyname in the groups dict will be "charA".
// One "Named" Capturing group and No g (global flag)

var reg = /(?<charA>a)/
st.match(reg)
// returns: 
["a", "a", index: 2, input: "i am iron man", groups: {"charA": "a"} ]

6. One “Named” Capturing group and No g (global flag)

This regex will return only the first match of the pattern

// Let's define this regex, 
// Match an a character in the string, globally (`g` flag given), 
// which means, if it occurs multiple times, 
// return it all those number of times.
// One "Named" Capturing group and No g (global flag)

var reg = /(?<charA>a)/g
st.match(reg)
// returns: 
["a", "a"]

Thanks, hope this was helpful.

Please leave us a comment if you spot any corrections, if you would like us to improve upon something or if you have anything to share!

Author: Admin

Hello Friends, welcome to Hackerdays.com. I am the Admin of the site. I have worked as a software programmer for 7 years. I have started this site to learn as well as share whatever little knowledge i have gained over the period of time. If you wish to contact me, you can drop in an email to: admin[AT]hackerdays[DOT]com or you can also use the contact page. Thank you and I'll see you around.

Let us know your thoughts: