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
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
  • Match everything enclosed
    (?:...)
  • Capture everything enclosed
    (...)
  • 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
No Match

/
/
gmi

Test String

Code Generator

Generated Code

const regex = /^(?:((?:IT|SM)\d{2}\s?[A-Z]{1}\d{3}\s?(\d{4}\s?){4}\d{3})|(NL\d{2}\s?[A-Z]{4}\s?(\d{4}\s?){2}\d{2})|(LV\d{2}\s?[A-Z]{4}\s?(\d{4}\s?){3}\d{1})|((?:BG|GB|IE)\d{2}\s?[A-Z]{4}\s?(\d{4}\s?){3}\d{2})|(GI\d{2}\s?[A-Z]{4}\s?(\d{4}\s?){3}\d{3})|(RO\d{2}\s?[A-Z]{4}\s?(\d{4}\s?){4})|(MT\d{2}\s?[A-Z]{4}\s?(\d{4}\s?){5}\d{3})|(NO\d{2}\s?(\d{4}\s?){2}\d{3})|((?:DK|FI|FO)\d{2}\s?(\d{4}\s?){3}\d{2})|((?:SI)\d{2}\s?(\d{4}\s?){3}\d{3})|((?:AT|EE|LU|LT)\d{2}\s?(\d{4}\s?){4})|((?:HR|LI|CH)\d{2}\s?(\d{4}\s?){4}\d)|((?:DE)\d{2}\s?(\d{4}\s?){4}\d{2})|((?:CZ|ES|SK|SE)\d{2}\s?(\d{4}\s?){5})|(PT\d{2}\s?(\d{4}\s?){5}\d)|((?:IS)\d{24})|((?:BE)\d{2}\s?(\d{4}\s?){3})|((?:FR|MC|GR)\d{2}\s?([0-9A-Z]{4}\s?){5}\d{3})|((?:PL|HU|CY)\d{2}\s?(\d{4}\s?){6}))$/gmi; // Alternative syntax using RegExp constructor // const regex = new RegExp('^(?:((?:IT|SM)\\d{2}\\s?[A-Z]{1}\\d{3}\\s?(\\d{4}\\s?){4}\\d{3})|(NL\\d{2}\\s?[A-Z]{4}\\s?(\\d{4}\\s?){2}\\d{2})|(LV\\d{2}\\s?[A-Z]{4}\\s?(\\d{4}\\s?){3}\\d{1})|((?:BG|GB|IE)\\d{2}\\s?[A-Z]{4}\\s?(\\d{4}\\s?){3}\\d{2})|(GI\\d{2}\\s?[A-Z]{4}\\s?(\\d{4}\\s?){3}\\d{3})|(RO\\d{2}\\s?[A-Z]{4}\\s?(\\d{4}\\s?){4})|(MT\\d{2}\\s?[A-Z]{4}\\s?(\\d{4}\\s?){5}\\d{3})|(NO\\d{2}\\s?(\\d{4}\\s?){2}\\d{3})|((?:DK|FI|FO)\\d{2}\\s?(\\d{4}\\s?){3}\\d{2})|((?:SI)\\d{2}\\s?(\\d{4}\\s?){3}\\d{3})|((?:AT|EE|LU|LT)\\d{2}\\s?(\\d{4}\\s?){4})|((?:HR|LI|CH)\\d{2}\\s?(\\d{4}\\s?){4}\\d)|((?:DE)\\d{2}\\s?(\\d{4}\\s?){4}\\d{2})|((?:CZ|ES|SK|SE)\\d{2}\\s?(\\d{4}\\s?){5})|(PT\\d{2}\\s?(\\d{4}\\s?){5}\\d)|((?:IS)\\d{24})|((?:BE)\\d{2}\\s?(\\d{4}\\s?){3})|((?:FR|MC|GR)\\d{2}\\s?([0-9A-Z]{4}\\s?){5}\\d{3})|((?:PL|HU|CY)\\d{2}\\s?(\\d{4}\\s?){6}))$', 'gmi') const str = `AD14 0008 0001 0012 3456 7890 BE23 0003 2399 3244 AT48 3200 0000 1234 5864 BE71 0961 2345 6769 BG18 RZBB 9155 0123 4567 89 HR17 2360 0001 1012 3456 5 CY21 0020 0195 0000 3570 0123 4567 CZ55 0800 0000 0012 3456 7899 DK95 2000 0123 4567 89 EE47 1000 0010 2014 5685 FI14 1009 3000 1234 58 FR76 3000 6000 0112 3456 7890 189 DE75 5121 0800 1245 1261 99 GI04 BARC 0000 0123 4567 890 GR96 0810 0010 0000 0123 4567 890 HU93 1160 0006 0000 0000 1234 5676 IS75 0001 1212 3456 3108 9620 99 IE64 IRCE 9205 0112 3456 78 IT60 X054 2811 1010 0000 0123 456 LV97 HABA 0012 3456 7891 0 LI74 0880 6123 4567 8901 2 LT60 1010 0123 4567 8901 LU12 0010 0012 3456 7891 MT31 MALT 0110 0000 0000 0000 0000 123 MC58 1009 6180 7901 2345 6789 085 NL02 ABNA 0123 4567 89 NO83 3000 1234 567 PL10 1050 0099 7603 1234 5678 9123 PT50 0027 0000 0001 2345 6783 3 RO09 BCYP 0000 0012 3456 7890 SM76 P085 4009 8121 2345 6789 123 SK89 7500 0000 0000 1234 5671 SI56 1920 0123 4567 892 ES79 2100 0813 6101 2345 6789 SE72 8000 0810 3400 0978 3242 CH56 0483 5012 3456 7800 9 GB33 BUKB 2020 1555 5555 55 VA59 0011 2300 0012 3456 78 AL35 2021 1109 0000 0000 0123 4567 AZ96 AZEJ 0000 0000 0012 3456 7890 BH02 CITI 0000 1077 1816 11 BA39 3385 8048 0021 1234 BR15 0000 0000 0000 1093 2840 814P 2 CR23 0151 0841 0026 0123 45 FO92 6460 0123 4567 89 GL89 6471 0123 4567 89 DO22 ACAU 0000 0000 0001 2345 6789 EG80 0002 0001 5678 9012 3451 8000 2 GE60 NB00 0000 0123 4567 89 GT20 AGRO 0000 0000 0012 3456 7890 IL17 0108 0000 0001 2612 345 JO71 CBJO 0000 0000 0000 1234 5678 90 KZ56 3190 0000 1234 4567 XK05 1212 0123 4567 8906 KW81 CBKU 0000 0000 0000 1234 5601 01 LB92 0007 0000 0000 1231 2345 6123 MK07 2000 0278 5123 453 MR13 0002 0001 0100 0012 3456 753 MU43 BOMM 0101 1234 5678 9101 000M UR MD21 EX00 0000 0000 0123 4567 ME25 5050 0001 2345 6789 51 PK36 SCBL 0000 0011 2345 6702 PS92 PALS 0000 0000 0400 1234 5670 2 QA54 QNBA 0000 0000 0000 6931 2345 6 LC14 BOSL 1234 5678 9012 3456 7890 1234 ST23 0002 0000 0289 3557 1014 8 SA44 2000 0001 2345 6789 1234 RS35 1050 0812 3123 1231 73 TL38 0010 0123 4567 8910 106 TN59 0401 8104 0049 4271 2345 TR32 0010 0099 9990 1234 5678 90 AE46 0090 0000 0012 3456 789 VG21 PACG 0000 0001 2345 6789 UA90 3052 9929 9000 4149 1234 5678 9 SC52 BAHL 0103 1234 5678 9012 3456 USD IQ20 CBIQ 8618 0010 1010 500 BY86 AKBB 1010 0000 0029 6600 0000 SV43 ACAT 0000 0000 0000 0012 3123 LY38 0210 0100 0000 1234 5678 9 SD88 1112 3456 7890 12 `; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // 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