Regular Expressions Quick Reference

Forgotten some of your regex syntax? This page is here to help. Not all syntax is supported by all regex implementations, so use the following key to check that the implementation you are using has the feature in question.
  • P - Supported By Perl Compatible Regular Expressions
  • P - Supported By POSIX Extended Regular Expressions
  • J - Supported By JAVA Regular Expressions
  • N - Supported By .NET Regular Expressions

Predefined Character Classes

. PPJN Matches any character, usually with the exception of the newline character.
\d PJN Matches any character that is a digit (0,1,2,3,4,5,6,7,8,9).
\D PJN Matches any character that is not a digit.
\w PJN Matches any character that is a digit, alphabetic letter or underscore.
\W PJN Matches any character that is not a digit, alphabetic letter or underscore.
\s PJN Matches any character that is whitespace (spaces, tabs, newlines, form feeds).
\S PJN Matches any character that is not whitespace.


User Defined Character Classes

[xyz] PPJN Matches any character that is an x, y or z.
[^xyz] PPJN Matches any character that is not an x, y or z.
[A-Z] PPJN Matches any character between A and Z (e.g. all uppercase characters).
[A-Za-z0-9_] PPJN Equivalent to \w.
[^A-Za-z0-9_] PPJN Equivalent to \W.


Quantifiers

? PPJN Greedy. Matches 1 or 0 of what precedes it - will always match 1 when it can, but will drop to matching 0 to make the regex match overall.
+ PPJN Greedy. Matches 1 or more of what precedes it - will match as many as it can, but will drop to matching less to make the regex match overall.
* PPJN Greedy. Matches 0 or more of what precedes it - will match as many as it can, but will drop to matching less or none to make the regex match overall.
{n} PPJN Matches n of what precedes it.
{n,m} PPJN Greedy. Matches between n and m of what precedes it - will match as many characters as possible (up to the limit given by m), but will drop to matching less (down to the limit given by n) to make the regex match overall.
?? PJN Minimal. Matches 1 or 0 of what precedes it - will always match nothing when it can, but will match once if required to make the regex match overall.
+? PJN Minimal. Matches 1 or more of what precedes it - will match as few times as it can while still allowing the regex match overall.
*? PJN Minimal. Matches 0 or more of what precedes it - will match as few times as it can while still allowing the regex match overall.
{n,m}? PJN Minimal. Matches between n and m of what precedes it - will match as few characters as possible (down to the limit given by n), but will match more (up to the limit given by n) to make the regex match overall.
?+ J Possesive. Matches 1 or 0 of what precedes it - will always match 1 when it can, even if this causes the regex not to match overall.
++ J Possesive. Matches 1 or more of what precedes it - will match as many as it can, even if this causes the regex not to match overall.
*+ J Possesive. Matches 0 or more of what precedes it - will match as many as it can, even if this causes the regex not to match overall.
{n,m}+ J Greedy. Matches between n and m of what precedes it - will match as many characters as possible (up to the limit given by m), but will not drop to matching less (down to the limit given by n) even if this causes the regex not to match overall.


Zero Width Assertions

\b PJN Matches where a word boundary occurs, but without matching a particular character in the string. \b would match at the start and end of the string too.
\B PJN Matches where a word boundary does not occur, but without matching a particular character in the string.
^ PPJN Matches at the start of a string or (under a certain modifier) the start of a line.
$ PPJN Matches at the end of a string or (under a certain modifier) the end of a line.


Grouping & Capturing

(...) PPJN Groups and captures whatever is between the brackets, and/or allows the whole group to be quantified if needed.
(?:...) PN Groups whatever is between the "(?:" and the ")" and allows the whole group to be quantified if needed. Does not capture.
(?...) J Groups whatever is between the "(?" and the ")" and allows the whole group to be quantified if needed. Does not capture.


Alternation

| PPJN Matches either what falls to the left of it, or what falls to the right of it. If it is inside a group (for example "a(bc|cb)d"), it is limited to the current group.


Quoting

\ PPJN Escapes the next character if it is a metacharacter. For example, "." will match any character, but "\." would match a literal ".". To put a literal "\" in a regex, use "\\".
\Q...\E PJ Treats everything between the \Q and the \E as literals rather than metacharacters, so no escaping of individual metacharacters is needed.


  User Comments


Anonymous
(Not rated)
(Report as abusive)
new String("123")????
new String(<literal>) is a newbie mistake... just use the literal directly.
Anonymous

(Report as abusive)
the comment above is incorrect
It is true that declaring variables unnecessarily is a frequent newbie mistake. However, in this case, it is necessary to have a String object to operate on.

For example, if you don't put the string in var text here, you can't use the replace method on it:

var text = "abababab";
var altered = text.replace(/b/, 'a');

Would you write that as
var altered = "abababab".replace(/b/, 'a');

I think not.

However, I do think that there is a typo in the telephone number example. The string is placed into a variable named "phone", so the second line should probably be:
var lastfour = phone.match(/\d{4}$/);

instead of
var lastfour = text.match(/\d{4}$/);

which (probably inadvertently) references a variable named "text" which is used in adjacent examples.
  View all   Rate and comment this article




 
Printer friendly version of the RegexQuickRef page



Advertisement



Free Magazine

Free Magazines
eWeek The essential technology information source for builders of e-business.... subscribe now

Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.