Hi all:
What's the intent of () inside the regex? Thanks.
pattern.replace(/\{(\d+)\}/g,
function(pattern, index) {
return args[index].toString();
});
PS: args is something like ["3", "dl1", "42"]
Hi all:
What's the intent of () inside the regex? Thanks.
pattern.replace(/\{(\d+)\}/g,
function(pattern, index) {
return args[index].toString();
});
PS: args is something like ["3", "dl1", "42"]
It is used to manage grouping.
The purpose of grouping is to make backreferences on searches & replaces. Using regex you can make that Jhon Doe
becomes Doe, Jhon
.
To achieve that, you would use a Regex (\w*) (\w*)
with two grups, and replace it for $2, $1
Usually, the first group (0) references the whole match of the regex, being the other groups numbered according to the order where they are in your expression.