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
No Match

/
/
gm

Test String

Code Generator

Generated Code

const regex = /^(?<rgb>rgb\((?<red>\d{1,3}),\s*\d{1,3},\s*\d{1,3}\))$|^(?<rgba>rgba\(\d{1,3},\s*\d{1,3},\s*\d{1,3},\s*[01](\.\d{1,2})?\))$|^(?<hsla>hsla\(\d{1,3},\s*\d{1,3}%,\s*\d{1,3}%,\s*[01](\.\d{1,2})?\))$|^(?<hsl>hsl\(\d{1,3},\s*\d{1,3}%,\s*\d{1,3}%\))$|^(?<hex>#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})(\d{2})?)$|^(?<cmyk>cmyk\(\d{1,3}%,\s*\d{1,3}%,\s*\d{1,3}%,\s*\d{1,3}%\))$/gm; // Alternative syntax using RegExp constructor // const regex = new RegExp('^(?<rgb>rgb\\((?<red>\\d{1,3}),\\s*\\d{1,3},\\s*\\d{1,3}\\))$|^(?<rgba>rgba\\(\\d{1,3},\\s*\\d{1,3},\\s*\\d{1,3},\\s*[01](\\.\\d{1,2})?\\))$|^(?<hsla>hsla\\(\\d{1,3},\\s*\\d{1,3}%,\\s*\\d{1,3}%,\\s*[01](\\.\\d{1,2})?\\))$|^(?<hsl>hsl\\(\\d{1,3},\\s*\\d{1,3}%,\\s*\\d{1,3}%\\))$|^(?<hex>#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})(\\d{2})?)$|^(?<cmyk>cmyk\\(\\d{1,3}%,\\s*\\d{1,3}%,\\s*\\d{1,3}%,\\s*\\d{1,3}%\\))$', 'gm') const str = `rgb(255,255,255) rgb(1,23,255) rgb(132,21,35) rgba(255,255,255,0.4) rgba(1,23,255,0.3) rgba(1,23,255,1) rgba(1,23,255,1.0) rgba(132,21,35,0.7) #403238 #ff323890 #ff890 #FF323890 hsl(240, 100%, 50%) hsl(248, 53%, 58%) hsl(147, 50%, 47%) hsla(9, 100%, 64%, 0.6) hsla(9, 100%, 64%, 0.8) hsla(9, 100%, 64%, 0.8) cmyk(0%, 100%, 100%, 0%) cmyk(0%,46%,51%,37%) // should fail rgba(255,255,255) rgba(1,23,255) rgba(132,21,35) rgba(1,23,255,0.) hsla(240, 100%, 50%) hsla(248, 53%, 58%) hsla(147, 50%, 47%) rgb(255,255,255,0.4) rgb(1,23,255,0.13) rgb(132,21,35,0.14) hsl(9, 100%, 64%, 0.6) hsl(9, 100%, 64%, 0.8) hsl(9, 100%, 64%, 0.8) #FG323890 #ars90 `; // Reset `lastIndex` if this regex is defined globally // regex.lastIndex = 0; 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