I don't get how I can limit a capturing group.
If I have a regex like this:
/^(\w{2,}\s\w{2,}){4,15}$/
I would think that this would capture any string with:
But the limiting of my capturing groups doesn't work. Can I limit capturing groups at all?
PS. I am using JavaScript to test the regexes in my examples.
Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d", "o", and "g".
What is Group in Regex? A group is a part of a regex pattern enclosed in parentheses () metacharacter. We create a group by placing the regex pattern inside the set of parentheses ( and ) . For example, the regular expression (cat) creates a single group containing the letters 'c', 'a', and 't'.
A backreference in a regular expression identifies a previously matched group and looks for exactly the same text again. A simple example of the use of backreferences is when you wish to look for adjacent, repeated words in some text. The first part of the match could use a pattern that extracts a single word.
This lookahead based regex should work for you:
/^(?=.{4,15}$)\w{2,}\s\w{2,}$/
Your regex: ^(\w{2,}\s\w{2,}){4,15}$
basically means there should be between 4 to 15 instances of a string containing 2 words with at least 2 characters separated by a space
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With