Join the Discussion
Up to now in this tutorial series we have looked at how to define single and multiple occurrences of single characters that we want to test for in our regular expression. The real power of regular expressions is its ability to handle complex pattern processing involving groups, backreferences, and lookaheads that allw us to test for multiple occurrences of multiple characters as a group either together or in separate spots within the text.
A group is defined by surrounding the characters that are to be grouped within parentheses.
var re = /(an)+/;
This regular expression tests for one or more repetitions (remember that is what + means) not of the single character "a" or the single character "n" (you can use [an]+ to do that). Instead it tests for one or more repetitions of the group "an". This expression would therefore match "ananan" but would not match "anaanna" (although it could match each of the two "an" that appear within it).
Groups can even be nested within groups so that you can have multiple occurrences of one group making up a part of a larger group.
var re = /((an)+b)+/;
This regular expression will match "anb", "ananb", "anbanb", and "ananbanbanananb" and any other similar character string.