Grouping parentheses in JavaScript regex

In the previous examples, the repetition operators (quantifiers) only acted on the single character that preceded them. What if we want to act on multiple characters with it?

For this, there are grouping parentheses '(' and ')'. They work like this: if something is in grouping parentheses and there is a repetition operator immediately after ')' - this operator will affect everything inside the parentheses.

Let's look at examples.

Example

In the following example, the search pattern looks like this: letter 'x', followed by the string 'ab' one or more times, then letter 'x':

let str = 'xabx xababx xaabbx' let res = str.replace(/x(ab)+x/g, '!');

As a result, the following will be written to the variable:

'! ! xaabbx'

Practical tasks

Given a string:

let str = 'ab abab abab abababab abea';

Write a regex that matches strings with a pattern: 'ab' string repeats 1 or more times.

enru