Regular Expressions 101

Save & Share

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8
  • .NET 7.0 (C#)
  • Rust
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests

Tools

Sponsors
There are currently no sponsors. Become a sponsor today!
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Non-capturing group
    (?:...)
  • Capturing group
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression

/
/
x

Test String

Code Generator

Generated Code

const regex = /^ (?&address) $ (?(DEFINE) (?<ALPHANUMERICE> [A-Za-z0-9!\#$%&'*+\-\/=?^_`{|}~] ) (?<CFWS> (?: (?: (?&FWS)? (?&comment) )++ (?&FWS)? ) | (?&FWS) ) (?<FWS> (?&WSPCRLF)++ ) (?<UTF8_extra_char> (?=[\x{C2}-\x{DF}\x{E0}-\x{EF}\x{F0}-\x{F4}]) (?: (?&UTF8_2) | (?&UTF8_3) | (?&UTF8_4) ) ) (?<UTF8_tail> [\x{80}-\x{BF}] ) (?<UTF8_2> [\x{C2}-\x{DF}] (?&UTF8_tail) ) (?<UTF8_3> \x{E0} [\x{A0}-\x{BF}] (?&UTF8_tail) | [\x{E1}-\x{EC}] (?&UTF8_tail){2} | \x{ED} [\x{80}-\x{9F}] (?&UTF8_tail) | [\x{EE}\x{EF}] (?&UTF8_tail){2} ) (?<UTF8_4> \x{F0} [\x{90}-\x{BF}] (?&UTF8_tail){2} | [\x{F1}-\x{F3}] (?&UTF8_tail){3} | \x{F4} [\x{80}-\x{8F}] (?&UTF8_tail){2} ) (?<VCHARWSP> [\x{21}-\x{7E}\ \t] ) (?<WSPCRLF> [\ \t\x{0D}\x{0A}] ) (?<address> (?&mailbox_list) | (?&group) ) (?<addr_spec> (?&local_part) @ (?&domain) ) (?<utf8_addr_spec> (?&utf8_local_part) @ (?&utf8_domain) ) (?<alt_address> (?&FWS) < (?&addr_spec) > ) (?<angle_addr> (?&CFWS)? < (?: (?&addr_spec) | (?&utf8_addr_spec) (?&alt_address)? | (?&obs_route)? (?&addr_spec) ) > (?&CFWS)? ) (?<atext> (?&ALPHANUMERICE) ) (?<utf8_atext> (?&ALPHANUMERICE) | (?&UTF8_extra_char) ) (?<atom> (?&CFWS)? (?&atext)++ (?&CFWS)? ) (?<utf8_atom> (?&CFWS)? (?&utf8_atext)++ (?&CFWS)? ) (?<utf8_ccontent> (?&ctext) | (?&utf8_quoted_pair) | (?&comment) ) (?<comment> \( (?: (?&FWS)? (?&utf8_ccontent) )*+ (?&FWS)? \) ) (?<ctext> [!-'*-\[\]-~] | (?&obs_ctext) | (?&UTF8_extra_char) ) (?<display_name> (?&phrase) ) (?<domain> (?&dot_atom) | (?&domain_literal) | (?&obs_domain) ) (?<utf8_domain> (?&utf8_dot_atom) | (?&domain_literal) | (?&obs_domain) ) (?<domain_literal> (?&CFWS)? \[ (?: (?&FWS)? (?&dtext) )*+ (?&FWS)? \] (?&CFWS)? ) (?<dot_atom> (?&CFWS)? (?&dot_atom_text) (?&CFWS)? ) (?<utf8_dot_atom> (?&CFWS)? (?&utf8_dot_atom_text) (?&CFWS)? ) (?<dot_atom_text> (?&atext)++ (?: \. (?&atext)++ )*+ ) (?<utf8_dot_atom_text> (?&utf8_atext)++ (?: \. (?&utf8_atext)++ )*+ ) (?<dtext> [!-Z^-~] | (?&obs_dtext) ) (?<group> (?&display_name) : (?&group_list)? ; (?&CFWS)? ) (?<group_list> (?&mailbox_list) | (?&CFWS) | (?&obs_group_list) ) (?<local_part> (?&dot_atom) | (?&quoted_string) | (?&obs_local_part) ) (?<utf8_local_part> (?&utf8_dot_atom) | (?&utf8_quoted_string) | (?&obs_local_part) ) (?<mailbox> (?&name_addr) | (?&addr_spec) | (?&utf8_addr_spec) ) (?<mailbox_list> (?: (?&mailbox) (?: , (?&mailbox) )*+ ) | (?&obs_mbox_list) ) (?<name_addr> (?&display_name)? (?&angle_addr) ) (?<obs_NO_WS_CTL> [\x{01}-\x{08}\x{0B}\x{0C}\x{0E}-\x{1F}\x{7F}] ) (?<obs_ctext> (?&obs_NO_WS_CTL) ) (?<obs_domain> (?&atom) (?: \. (?&atom) )*+ ) (?<obs_domain_list> (?: (?&CFWS) | , )*+ @ (?&domain) (?: , (?&CFWS)? (?: @ (?&domain) )? )*+ ) (?<obs_dtext> (?&obs_NO_WS_CTL) | (?&quoted_pair) ) (?<obs_group_list> (?: (?&CFWS)? , )++ (?&CFWS)? ) (?<obs_local_part> (?&word) (?: \. (?&word) )*+ ) (?<obs_mbox_list> (?: (?&CFWS)? , )*+ (?&mailbox) (?: , (?: (?&mailbox) | (?&CFWS))? )*+ ) (?<obs_phrase> (?&word) (?: (?&word) | \. | (?&CFWS) )*+ ) (?<obs_qp> \\ (?: \x{00} | (?&obs_NO_WS_CTL) | [\x{0D}\x{0A}] ) ) (?<obs_qtext> (?&obs_NO_WS_CTL) ) (?<obs_route> (?&obs_domain_list) : ) (?<phrase> (?&word)++ | (?&obs_phrase) ) (?<qcontent> (?&utf8_qcontent) ) (?<utf8_qcontent> (?&utf8_qtext) | (?&utf8_quoted_pair) ) (?<qtext> [!\#-\[\]-~] | (?&obs_qtext) ) (?<utf8_qtext> (?&qtext) | (?&UTF8_extra_char) ) (?<quoted_pair> \\ (?&VCHARWSP) | (?&obs_qp) ) (?<utf8_quoted_pair> \\ (?&utf8_text) | (?&obs_qp) ) (?<utf8_text> [\x{01}-\x{09}\x{0D}\x{0A}\x{0E}-\x{7F}] | (?&UTF8_extra_char) ) (?<quoted_string> (?&CFWS)? " (?: (?&FWS)? (?&qcontent) )*+ (?&FWS)? " (?&CFWS)? ) (?<utf8_quoted_string> (?&CFWS)? " (?: (?&FWS)? (?&utf8_qcontent) )*+ (?&FWS)? " (?&CFWS)? ) (?<word> (?&utf8_atom) | (?&utf8_quoted_string) ) )/; // Alternative syntax using RegExp constructor // const regex = new RegExp('^ (?&address) $ (?(DEFINE) (?<ALPHANUMERICE> [A-Za-z0-9!\\#$%&\'*+\\-\\\/=?^_`{|}~] ) (?<CFWS> (?: (?: (?&FWS)? (?&comment) )++ (?&FWS)? ) | (?&FWS) ) (?<FWS> (?&WSPCRLF)++ ) (?<UTF8_extra_char> (?=[\\x{C2}-\\x{DF}\\x{E0}-\\x{EF}\\x{F0}-\\x{F4}]) (?: (?&UTF8_2) | (?&UTF8_3) | (?&UTF8_4) ) ) (?<UTF8_tail> [\\x{80}-\\x{BF}] ) (?<UTF8_2> [\\x{C2}-\\x{DF}] (?&UTF8_tail) ) (?<UTF8_3> \\x{E0} [\\x{A0}-\\x{BF}] (?&UTF8_tail) | [\\x{E1}-\\x{EC}] (?&UTF8_tail){2} | \\x{ED} [\\x{80}-\\x{9F}] (?&UTF8_tail) | [\\x{EE}\\x{EF}] (?&UTF8_tail){2} ) (?<UTF8_4> \\x{F0} [\\x{90}-\\x{BF}] (?&UTF8_tail){2} | [\\x{F1}-\\x{F3}] (?&UTF8_tail){3} | \\x{F4} [\\x{80}-\\x{8F}] (?&UTF8_tail){2} ) (?<VCHARWSP> [\\x{21}-\\x{7E}\\ \\t] ) (?<WSPCRLF> [\\ \\t\\x{0D}\\x{0A}] ) (?<address> (?&mailbox_list) | (?&group) ) (?<addr_spec> (?&local_part) @ (?&domain) ) (?<utf8_addr_spec> (?&utf8_local_part) @ (?&utf8_domain) ) (?<alt_address> (?&FWS) < (?&addr_spec) > ) (?<angle_addr> (?&CFWS)? < (?: (?&addr_spec) | (?&utf8_addr_spec) (?&alt_address)? | (?&obs_route)? (?&addr_spec) ) > (?&CFWS)? ) (?<atext> (?&ALPHANUMERICE) ) (?<utf8_atext> (?&ALPHANUMERICE) | (?&UTF8_extra_char) ) (?<atom> (?&CFWS)? (?&atext)++ (?&CFWS)? ) (?<utf8_atom> (?&CFWS)? (?&utf8_atext)++ (?&CFWS)? ) (?<utf8_ccontent> (?&ctext) | (?&utf8_quoted_pair) | (?&comment) ) (?<comment> \\( (?: (?&FWS)? (?&utf8_ccontent) )*+ (?&FWS)? \\) ) (?<ctext> [!-\'*-\\[\\]-~] | (?&obs_ctext) | (?&UTF8_extra_char) ) (?<display_name> (?&phrase) ) (?<domain> (?&dot_atom) | (?&domain_literal) | (?&obs_domain) ) (?<utf8_domain> (?&utf8_dot_atom) | (?&domain_literal) | (?&obs_domain) ) (?<domain_literal> (?&CFWS)? \\[ (?: (?&FWS)? (?&dtext) )*+ (?&FWS)? \\] (?&CFWS)? ) (?<dot_atom> (?&CFWS)? (?&dot_atom_text) (?&CFWS)? ) (?<utf8_dot_atom> (?&CFWS)? (?&utf8_dot_atom_text) (?&CFWS)? ) (?<dot_atom_text> (?&atext)++ (?: \\. (?&atext)++ )*+ ) (?<utf8_dot_atom_text> (?&utf8_atext)++ (?: \\. (?&utf8_atext)++ )*+ ) (?<dtext> [!-Z^-~] | (?&obs_dtext) ) (?<group> (?&display_name) : (?&group_list)? ; (?&CFWS)? ) (?<group_list> (?&mailbox_list) | (?&CFWS) | (?&obs_group_list) ) (?<local_part> (?&dot_atom) | (?&quoted_string) | (?&obs_local_part) ) (?<utf8_local_part> (?&utf8_dot_atom) | (?&utf8_quoted_string) | (?&obs_local_part) ) (?<mailbox> (?&name_addr) | (?&addr_spec) | (?&utf8_addr_spec) ) (?<mailbox_list> (?: (?&mailbox) (?: , (?&mailbox) )*+ ) | (?&obs_mbox_list) ) (?<name_addr> (?&display_name)? (?&angle_addr) ) (?<obs_NO_WS_CTL> [\\x{01}-\\x{08}\\x{0B}\\x{0C}\\x{0E}-\\x{1F}\\x{7F}] ) (?<obs_ctext> (?&obs_NO_WS_CTL) ) (?<obs_domain> (?&atom) (?: \\. (?&atom) )*+ ) (?<obs_domain_list> (?: (?&CFWS) | , )*+ @ (?&domain) (?: , (?&CFWS)? (?: @ (?&domain) )? )*+ ) (?<obs_dtext> (?&obs_NO_WS_CTL) | (?&quoted_pair) ) (?<obs_group_list> (?: (?&CFWS)? , )++ (?&CFWS)? ) (?<obs_local_part> (?&word) (?: \\. (?&word) )*+ ) (?<obs_mbox_list> (?: (?&CFWS)? , )*+ (?&mailbox) (?: , (?: (?&mailbox) | (?&CFWS))? )*+ ) (?<obs_phrase> (?&word) (?: (?&word) | \\. | (?&CFWS) )*+ ) (?<obs_qp> \\\\ (?: \\x{00} | (?&obs_NO_WS_CTL) | [\\x{0D}\\x{0A}] ) ) (?<obs_qtext> (?&obs_NO_WS_CTL) ) (?<obs_route> (?&obs_domain_list) : ) (?<phrase> (?&word)++ | (?&obs_phrase) ) (?<qcontent> (?&utf8_qcontent) ) (?<utf8_qcontent> (?&utf8_qtext) | (?&utf8_quoted_pair) ) (?<qtext> [!\\#-\\[\\]-~] | (?&obs_qtext) ) (?<utf8_qtext> (?&qtext) | (?&UTF8_extra_char) ) (?<quoted_pair> \\\\ (?&VCHARWSP) | (?&obs_qp) ) (?<utf8_quoted_pair> \\\\ (?&utf8_text) | (?&obs_qp) ) (?<utf8_text> [\\x{01}-\\x{09}\\x{0D}\\x{0A}\\x{0E}-\\x{7F}] | (?&UTF8_extra_char) ) (?<quoted_string> (?&CFWS)? " (?: (?&FWS)? (?&qcontent) )*+ (?&FWS)? " (?&CFWS)? ) (?<utf8_quoted_string> (?&CFWS)? " (?: (?&FWS)? (?&utf8_qcontent) )*+ (?&FWS)? " (?&CFWS)? ) (?<word> (?&utf8_atom) | (?&utf8_quoted_string) ) )', '') const str = `John <a@b.c>, w@com, "wibb.[dhdhd]"@(comment)fred.com`; // Reset `lastIndex` if this regex is defined globally // regex.lastIndex = 0; let m; if ((m = regex.exec(str)) !== null) { // The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); }

Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions