We can use parentheses to repeat a match so that the same text is matched in more than one place in the expression. It is not the pattern that is repeated but the matched value. We do this by referring to the sub-expression according to its indexed location within the pattern. The index is incremented for every left parenthesis encountered. The first is referred to as \1, the second as \2 and so on. This is useful for balancing matching quote symbols around a text string that might have either single or double quote marks around it. Unless we can relate the matches at each end, we might find that we do in fact have either one of the two quote symbols but we don't necessarily have a balanced pair.
This matches a single or double quote character:
/['"]/
This matches a single or double quote character at either end of any other character sequence:
/['"].*['"]/
However, the text between the quotes could contain a quote so we'll replace the match between them with any non-quote character. Like this:
/['"][^'"]*['"]/
To use a reference to a previous sub-expression, we need to mark the sub-expression with parentheses:
/(['"])[^'"]*['"]/
Now we can refer to the first sub-expression at a later stage and require that the same characters be repeated:
/(['"])[^'"]*\1/
This ensures that the sequence of characters 'AAA' would match but 'AAA" would not even though "AAA" would.
A grouped sub-expression can be prevented from being indexed by placing a question mark and colon immediately inside the parentheses and then the item cannot be indexed as a reference. This works in JavaScript version 1.3 onwards.
Beware that you do not use an index greater than the number of sub-patterns available. The backslash and digit will be interpreted as a character escape otherwise.
Because the backslash can also be interpreted as a character escape you must be unambiguous as to your intentions. To ensure that it is interpreted as a character escape, use a value that is more than one digit long.
Prev | Home | Next |
RegExp pattern - position | Up | RegExp pattern - repetition |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |