Subexpression groups inside parentheses within a bigger regular expression can be referenced later in the regular expression. The regular expression /((a|b)|cd)(ef|gh)/ has three groups (a|b), ((a|b)|cd) and (ef|gh). Note that one of them, ((a|b)|cd), is a nested group.
To match an email address of the form abc@xyz.com, you can use the pattern
/([a-z][\w]*)@(([a-z0-9]+)[.]([a-z]{2,63}))/
It contains the following subexpression groups:
- The email id
([a-z][\w]*) - The domain name
([a-z0-9]+)[.]([a-z]{2,63})) - The second level domain
([a-z0-9]+)which is part (nested group) of the domain name - The top level domain
([a-z]{2,63})which again is part of the domain name
The subpatterns with the parentheses can be later referenced in the regular expression by a backslash (\) followed by a number like \1, \2 etc. The number refers to the position of the group among all groups in the whole regex. Thus \1 refers to the first subexpression group, \2 refers to the second subexpression group etc. Since the groups can be nested, the position of the left parentheses decides the number. Thus in /([a-z][\w]*)@(([a-z0-9]+)[.]([a-z]{2,63}))/
\1is([a-z][\w]*)\2is([a-z0-9]+)[.]([a-z]{2,63}))\3is([a-z0-9]+)\4is([a-z]{2,63})
A reference to a subexpression group later in the regular expression does not refer to the pattern contained in that group, but to the text that matched the pattern. In the pattern /(\d+)xy\1/, the \1 refers to the text match by the first group (\d+). So if the target string is "abc0384xy0384pqr", then \1 is the string '0384'. The \1 enforces that the text matched by the first group (\d+), in this case ‘0384‘, must occur later where \1 is used.
let wordPttn = /(\d+)xy\1/; let targetString = "abc0384xy0384pqr";let result = targetString.match(wordPttn);console.log((result !== null ? `Pattern ${wordPttn} FOUND in '${targetString}'. Matched string is '${result[0]}'` : `Pattern ${wordPttn} NOT FOUND in '${targetString}'`));targetString = "abc0384xy038pqr";result = targetString.match(wordPttn);console.log((result !== null ? `Pattern ${wordPttn} FOUND in '${targetString}'. Matched string is '${result[0]}'` : `Pattern ${wordPttn} NOT FOUND in '${targetString}'`));
Output:
Pattern /(\d+)xy\1/ FOUND in 'abc0384xy0384pqr'. Matched string is '0384xy0384'
Pattern /(\d+)xy\1/ NOT FOUND in 'abc0384xy038pqr'
The pattern /['"][^'"]*['"]/ matches text within quotes but it does not enforce the opening and closing quotes to match because [‘”] only enforces that it can be either a single quote (‘) or a double quote(“) but does not enforce that the ending quote should match the starting quote. You can achieve that constrainst with a back reference – /(['"])[^'"]*\1/.
let wordPttn = /(['"])[^'"]*\1/; let targetString = '"This text is in double quotes" but this is not';let result = targetString.match(wordPttn);console.log((result !== null ?`Pattern ${wordPttn} FOUND in '${targetString}'. Matched string is '${result[0]}'`: `Pattern ${wordPttn} NOT FOUND in '${targetString}'`));
Output:
Pattern /(['"])[^'"]*\1/ FOUND in '"This text is in double quotes" but this is not'. Matched string is '"This text is in double quotes"'
But this still does not allow single quotes within double quotes and vice versa.
let wordPttn = /(['"])[^'"]*\1/; let targetString = '"single quotes (\') are not allowed in double quotes" and vice versa';;result = targetString.match(wordPttn);console.log((result !== null ? `Pattern ${wordPttn} FOUND in '${targetString}'. Matched string is '${result[0]}'` : `Pattern ${wordPttn} NOT FOUND in '${targetString}'`));
Output:
Pattern /(['"])[^'"]*\1/ NOT FOUND in '"single quotes (') are not allowed in double quotes" and vice versa'
Double quotes can be escaped within double quotes and so can be single quotes within single quotes.
let wordPttn = /(['"])[^'"]*\1/; let targetString = "\"Double quotes can be escaped within double quotes\" and so can be single quotes within single quotes";;result = targetString.match(wordPttn);console.log((result !== null ? `Pattern ${wordPttn} FOUND in '${targetString}'. Matched string is '${result[0]}'` : `Pattern ${wordPttn} NOT FOUND in '${targetString}'`));
Output:
Pattern /(['"])[^'"]*\1/ FOUND in '"Double quotes can be escaped within double quotes" and so can be single quotes within single quotes'. Matched string is '"Double quotes can be escaped within double quotes"'
You can create a grouped subexpression in parentheses without creating a numbered reference. Just add ‘?:’ after the left parenthesis like /(?:abc)xyz/. Here (?:abc) is used only for grouping but a numbered reference is not created for it.
In the pattern /([a-z][\w]*)@(?:(?:[a-z0-9]+)[.]([a-z]{2,63}))/, both the subpatterns (?:(?:[a-z0-9]+)[.]([a-z]{2,63})) and (?:[a-z0-9]+) are used only for grouping but not for creating numbered references because there is a ‘?:’ after the left parenthesis. So the only references created are
\1for([a-z][\w]*)\2for([a-z]{2,63})