Search within TutorialsPark

Javascript Regular Expressions: Metacharacters Applications

Learn to apply Regular Expression Metacharacters for practical purposes.

Javascript Regular Expressions: MetaCharacters for repeated pattern Matching

The metacharacters can also be used to match more than one characters, for that purpose we use what is called as quantifiers

For eg: to match multiple uppercase letters , the regular expression to be used is /[A-Z]+/ here the + sign means "one or more than one of previous characters". The table below provides a list of all quantifiers.

Table: Regular Expression : Quantifiers - Greedy Metacharacters.

Metacharacter Description
x? To match 0 or 1 of x
(xyz)? To match zero or one pattern of xyz.
x* To match 0 or more of x
(xyz)* To match zero or more patterns of xyz
x+ To match 1 or more of x
(xyz)+ To match one or more patterns of xyz
x{m,n} To match at least m of x and no more than n of x.

Javascript Regular Expressions Metacharacters: Greed Factor

A quantifier is called as greedy if it matches the largest possible set of characters from left to right, checking the last possible character that satisfies the pattern.

The demo below , the regular expression /[A-Z][a-z]*\s checks for an uppercase letter [A-Z] , followed by a zero or more lowercase characters [a-z]* and a space, \s. There can be zero or more lowercase characters.

Example: JavaScript Regular Expressions Quantifiers: Greedy Metacharacters.

Give it a TRY! » Note:The text must have one uppercase characters, zero or more lowercase characters and a space .

JavaScript Regular Expressions: Switching off the Greed

The greed can be turned off by simply placing a question mark "?" after a greed quantifier. It causes the search result to end after the first match rather than the last one.

Example: JavaScript Regular Expression: Metacharacters to turn off Greediness

Give it a TRY! » Note: The regex search stops when it finds first lowercase letter, and then replaced that letter with ZZZ.

Javascript Regular Expression: Anchoring Metacharacters

At times a metacharacter needs to be anchored, so that it matches only if the specified pattern is found at the start or end of the string.

These metacharacters are used to look for characters based on position just to the left or to the right of the character being matched.

In technical terms anchors are called as zero-width assertions because they denote positions and not actual characters within a string.

Table: Regular Expression : - Anchoring Metacharacters.

Metacharacter Description
^ To match the beginnig of the string
$ To match the end of string
\b To match a word boundry
\B To match a nonword boundry

Example and Format: JavaScript Regular Expression - Anchoring Metacharacters.

Give it a TRY! » Note:The string beginning with This will be matched only.

JavaScript Regular Expressions: Alternation

Inorder to match alternative patterns using regular expression we use the method of Alternation. Each of the alternative expressions is seperated by a vertical bar.

Eg: The regular expression /Mars|Venus|Jupiter/ will match a string containing Mars or Venus or Jupiter

Example: JavaScript Regular Expression: Alternation

Give it a TRY! » Note: The test is case sensitive.

JavaScript Regular Expressions: Alternation - Grouping or Clustering

Grouping and clustering is done by grouping a set of characters within parentheses.It creates a subpattern. Eg: /(ha)/

By using this method instead of matching on zero, one or more of previous single characters, the previous subpattern can be matched.Alternation can also be controlled in same way using parentheses.

Eg: /(ha)+/ can search for ha , haha and so forth, without parentheses it would search for "h" followed by one or more occurences of "a" like "ha", "haaaa".

Example: JavaScript Regular Expression: Grouping or Clustering

Give it a TRY! » Note: The alternative is either iPhone, iPad or iMac, ^ is used to match pattern at the start of line.

JavaScript Regular Expressions: Remembering and Capturing

When a regular expression pattern is enclosed in parentheses, the subpattern created is captured i.e the subpattern is saved in special numbered class properties, beginning with $1, $2 and so on.

For eg: if the regular expression is /(mars)(jupiter)/, "mars" will be captured in $1 and "jupiter" in $2.

Example: JavaScript Regular Expression: Alternation

Give it a TRY! » Note: $1 represents the first captured subpattern and $2 represents the second subpattern.