Search This Blog

2024/03/27

Javascript Regular Expression:Named Capturing Group

Numbered capture groups allow one to refer to certain portions of a string
that a regular expression matches. Each capture group is assigned a unique
number and can be referenced using that number, but this can make a regular
expression hard to grasp and refactor.

For example, given /(\d{4})-(\d{2})-(\d{2})/ that matches a date, one cannot
be sure which group corresponds to the month and which one is the day without
examining the surrounding code. Also, if one wants to swap the order of the
month and the day, the group references should also be updated.

Code:
let re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/u;
let result = re.exec("2015-01-02");
console.log(result);

//accesing by name
console.log("year:" + result.groups.year);
console.log("month:" + result.groups.month);
console.log("day:" + result.groups.day);

//accesing by numberic index
console.log("year:" + result[1]);
console.log("month:" + result[2]);
console.log("day:" + result[3]);
Output:
[
'2015-01-02',
'2015',
'01',
'02',
index: 0,
input: '2015-01-02',
groups: [Object: null prototype] {
                    year: '2015',
                    month: '01',
                    day: '02'
                }
]
year:2015
month:01
day:02
year:2015
month:01
day:02


Explanation:
Named groups can be accessed from properties of a groups property of
the regular expression result. Numbered references to the groups are
also created, just as for non-named groups.

Note:
/u in the pattern at the end enable full Unicode matching.It's
important to note that the \u escape sequence is specific to
JavaScript regular expressions and is not part of the broader regular
expression syntax used in other programming languages or tools.

Named Group & Replacement Target:
Named groups can be referenced from the replacement value passed to
String.prototype.replace too. If the value is a string, named groups
can be accessed using the $<name> syntax.

Code:
let re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/u;
let result = '2015-01-02'.replace(re, '$<day>/$<month>/$<year>');
console.log(result)
Output:
02/01/2015

Named Capturing Group & Back Reference:
A named group can be accessed within a regular expression via the \k<name>
construct.
For example

Code:
let duplicate = /^(?<half>.*).\k<half>$/u;
console.log(duplicate.test('a*b')); // false
console.log(duplicate.test('a*a')); // true
console.log(duplicate.test('abc*abc')); // true
console.log(duplicate.test('abc=abc')); // true
console.log(duplicate.test('abc==abc')); // false
console.log(duplicate.test('abc:=abc')); // false
console.log(duplicate.test('aaaaaaa')); // true
Output:
false
true
true
true
false
false
true
Note:
Basically regular expression /^(?<half>.*).\k<half>$/u; stands for
1) Any character any number of time e.g. say 'abc'
2) Followed by any character e.g. say '='
3) Followed by same sequence of charcters in 1st condition.then it
should be same as 1st condition so 'abc' again

so final string will be abc=abc


Back Reference with Named Capturing Group & Numbered References:
Named references can also be used simultaneously with numbered references.

Code:
let triplicate = /^(?<part>.*).\k<part>.\1$/u;
       // all three parts should be same hence true
console.log(triplicate.test('a*a*a'));
// all three parts are not same hence false
console.log(triplicate.test('a*a*b'));

//here seperator is same char and all three parts are same abc
console.log(triplicate.test('abc*abc*abc'));

/*no need to have same seperator char but all
three parts need to be same abc*/
console.log(triplicate.test('abc*abc#abc'));
Output:
true
false
true
true


No comments:

Post a Comment