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

/
/
gmi

Test String

Code Generator

Generated Code

const regex = /((a-zA-Z){2}\/)?(app|album|book|audiobook|author|movie|movie-collection|studio|artist|developer|music-video|video|podcast|tv-season|tv-show)\/((a-zA-Z0-9\-)*\/)?(id)?(0-9)*/gmi; // Alternative syntax using RegExp constructor // const regex = new RegExp('((a-zA-Z){2}\\\/)?(app|album|book|audiobook|author|movie|movie-collection|studio|artist|developer|music-video|video|podcast|tv-season|tv-show)\\\/((a-zA-Z0-9\\-)*\\\/)?(id)?(0-9)*', 'gmi') const str = `//bad link examples from past traffic: /bkp.sql.tar.gz /bkp.sql.tar.gz /core/modules/system/tests/modules/error_service_test/src/Logger /_get/spool/kjax/rrmjaxfl.923e30d17ca8f0b37a489c818eae0577.txt.js /_get/spool/kjax/rrmjaxfl.923e30d17ca8f0b37a489c818eae0577.txt.php /?AT=10l9IM&i=469623817&uo=4/cat ../../../../../../../../etc/passwd /?at=11lK2U/..///////..////..//////etc/passwd&forcetoken=11lK2U/..///////..////..//////etc/passwd //can't do anything with these, no point in calling us /subscribe?app=music&at=1001lLm9&itscg=30200&itsct=addotnet&ct=884-11165151 /lookup?id=622463230 /search?term=You're%20Not%20There%20-%20Lukas%20Graham&entity=song&media=music&country=DK&limit=1 /WebObjects/MZSearch.woa/wa/advancedSearch?s=143443&partnerId=2003&affToken=www.last.fm&at=10l3Sh&artistTerm=Schmetterlinge&songTerm=Wer+schreibt+die+Geschichte? /actuator/jolokia //supported by GeniusLink /app/id1235 /us/app/id1235 /us/app/among-us/id1351168404 /us/app/1password-7-password-manager/id1333542190?mt=12 /us/book/dune/id597944491 /us/audiobook/a-promised-land-unabridged/id1540585069 /us/author/c.j.-archer/id421351544?mt=11 /us/movie/tenet/id1538251548 /us/movie-collection/the-lord-of-the-rings-extended-editions-bundle/id1537502630 /us/studio/pixar/id81758682 /us/album/willow/1544268281?i=1544268298 /us/album/evermore/1544268281 /us/artist/taylor-swift/159260351 /us/music-video/i-dont-care-bonus-video/1445881948 /au/video/id254295715?mt=5 /us/podcast/the-chernobyl-podcast/id1459712981 /us/tv-season/freight-trains-and-monsters/id1514426968?i=1520794309 /us/tv-season/yellowstone-season-3/id1514426968 /ca/tv-show/come-fly-with-me/id410581843 /us/developer/tapbots/id293642940 /video/histories/id253624330?uo=5&AT=10l9IM /video/course-you-know-this-means/id665084512?uo=5&AT=10l9IM /artist/jaime-urrutia/id27431058?uo=5&at=1010l34Ub&ct=cart&app=music /app/noteshelf-2/id1271086060?mt=8&uo=4&at=11l9z8 /album/1552222070?uo=4&app=itunes&at=1l3vpUI&lId=22814830&cId=none&sr=6&src=Linkfire&itscg=30440&itsct=catchall_p6&ct=LFV_44d9b6dc091636bfbe66b473b4135330&ls=1 `; 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