Online
 
Sunday, 23 November 2008
 
 

JavaScript - Grouping and alternation | Print |  E-Mail
 

Grouping and alternation

Regular expressions use parentheses to group subexpressions, just as mathematical expressions do. Parentheses are useful, for example, to allow a repetition character to be applied to an entire subexpression. They are also useful with the | character, which is used to separate alternatives. Parenthesized groups have a special behavior: when a pattern match is found, the text that matches each group is saved and can be referred to by group number. The following table summarizes this syntax.

Character

Meaning

a | b

Match either a or b

(sub)

Group subexpression sub into a single term, and remember the text that it matched

(?:sub)

Group subexpression sub but do not number the group or remember the text it matches (JS 1.5)

\n

Match exactly the same characters that were matched by group number n

$n

In replacement strings, substitute the text that matched the nth subexpression

 

This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Jumbo Coklat
 
Top! Top!