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

/
/
gxm

Test String

Code Generator

Generated Code

const regex = /^[ \t]*(?:(?<label>[a-zA-Z0-9_]+):[ \t]*)? (?: (?<op>[a-zA-Z0-9_\.]+) (?: (?: (?: [ \t]* (?: (?<h0>[-+]?[0oO]?[xX][a-fA-F0-9]{1,8}) | (?<o0>[-+]?[0oO][0-7]+) | (?<b0>[-+]?[0oO]?[bB][01]{1,32}) | (?<l0>[a-zA-Z0-9_]+) | (?<d0>[-+]?\#?\d+) ) [ \t]* (?:\((?<r0b>\$[a-zA-Z0-9_]{2,5})\))?[ \t]* ) | [ \t]*(?<r0>\$[a-zA-Z0-9_]{2,5})[ \t]* (?: (?: ,[ \t]* (?: (?<d1>[-+]?\#?\d+) | (?<h1>[-+]?[0oO]?[xX][a-fA-F0-9]{1,8}) | (?<o1>[-+]?[0oO][0-7]+) | (?<b1>[-+]?[0oO]?[bB][01]{1,32}) | (?<l1>[a-zA-Z0-9_]+) ) [ \t]* (?:\((?<r1b>\$[a-zA-Z0-9_]{2,5})\))?[ \t]* ) | ,[ \t]*(?<r1>\$[a-zA-Z0-9_]{2,5})[ \t]* (?: (?: ,[ \t]* (?: (?<d2>[-+]?\#?\d+) | (?<h2>[-+]?[0oO]?[xX][a-fA-F0-9]{1,8}) | (?<o2>[-+]?[0oO][0-7]+) | (?<b2>[-+]?[0oO]?[bB][01]{1,32}) | (?<l2>[a-zA-Z0-9_]+) ) [ \t]* (?:\((?<r2b>\$[a-zA-Z0-9_]{2,5})\))?[ \t]* ) | ,[ \t]*(?<r2>\$[a-zA-Z0-9_]{2,5})[ \t]* )? )? )? )? )? (?<pseudo_op> \. (?<name>[a-zA-Z]+) [ \t]* (?<arg> (?<hp0>[-+]?[0oO]?[xX][a-fA-F0-9]{1,8}) | (?<bp0>[-+]?[0oO]?[bB][01]{1,32}) | (?<op0>[-+]?[0oO][0-7]+) | (?<dp0>[-+]?\#?\d+) | (?<lp0>[a-zA-Z0-9_]+) ) [ \t]* )? (?:[ \t]*(?<comment>[!;].*)?) $/gm; // Alternative syntax using RegExp constructor // const regex = new RegExp('^[ \\t]*(?:(?<label>[a-zA-Z0-9_]+):[ \\t]*)? (?: (?<op>[a-zA-Z0-9_\\.]+) (?: (?: (?: [ \\t]* (?: (?<h0>[-+]?[0oO]?[xX][a-fA-F0-9]{1,8}) | (?<o0>[-+]?[0oO][0-7]+) | (?<b0>[-+]?[0oO]?[bB][01]{1,32}) | (?<l0>[a-zA-Z0-9_]+) | (?<d0>[-+]?\\#?\\d+) ) [ \\t]* (?:\\((?<r0b>\\$[a-zA-Z0-9_]{2,5})\\))?[ \\t]* ) | [ \\t]*(?<r0>\\$[a-zA-Z0-9_]{2,5})[ \\t]* (?: (?: ,[ \\t]* (?: (?<d1>[-+]?\\#?\\d+) | (?<h1>[-+]?[0oO]?[xX][a-fA-F0-9]{1,8}) | (?<o1>[-+]?[0oO][0-7]+) | (?<b1>[-+]?[0oO]?[bB][01]{1,32}) | (?<l1>[a-zA-Z0-9_]+) ) [ \\t]* (?:\\((?<r1b>\\$[a-zA-Z0-9_]{2,5})\\))?[ \\t]* ) | ,[ \\t]*(?<r1>\\$[a-zA-Z0-9_]{2,5})[ \\t]* (?: (?: ,[ \\t]* (?: (?<d2>[-+]?\\#?\\d+) | (?<h2>[-+]?[0oO]?[xX][a-fA-F0-9]{1,8}) | (?<o2>[-+]?[0oO][0-7]+) | (?<b2>[-+]?[0oO]?[bB][01]{1,32}) | (?<l2>[a-zA-Z0-9_]+) ) [ \\t]* (?:\\((?<r2b>\\$[a-zA-Z0-9_]{2,5})\\))?[ \\t]* ) | ,[ \\t]*(?<r2>\\$[a-zA-Z0-9_]{2,5})[ \\t]* )? )? )? )? )? (?<pseudo_op> \\. (?<name>[a-zA-Z]+) [ \\t]* (?<arg> (?<hp0>[-+]?[0oO]?[xX][a-fA-F0-9]{1,8}) | (?<bp0>[-+]?[0oO]?[bB][01]{1,32}) | (?<op0>[-+]?[0oO][0-7]+) | (?<dp0>[-+]?\\#?\\d+) | (?<lp0>[a-zA-Z0-9_]+) ) [ \\t]* )? (?:[ \\t]*(?<comment>[!;].*)?) $', 'gm') const str = `!============================================================ ! CS-2200 Homework 1 ! ! Please do not change main's functionality, ! except to change the argument for factorial or to meet your ! calling convention !============================================================ main: la \$sp, stack ! load address of stack label into \$sp lw \$sp, 0(\$sp) ! FIXME: load desired value of the stack ! (defined at the label below) into \$sp la \$at, factorial ! load address of factorial label into \$at addi \$a0, \$zero, 5 ! \$a0 = 5, the number to factorialize jalr \$at, \$ra ! jump to factorial, set \$ra to return addr halt ! when we return, just halt factorial: sw \$a0, 0(\$sp) ! put i, the first parameter, on stack addi \$sp, \$sp, -4 ! leave space for argument, return value, return address, and old frame pointer sw \$fp, 1(\$sp) ! store old frame pointer addi \$fp, \$sp, 0 ! frame pointer points to first local variable sw \$ra, 2(\$fp) ! store return address lw \$t0, 4(\$fp) ! load i to \$t0 beq \$t0, \$zero, base ! if i == 0, branch to base case addi \$a0, \$t0, -1 ! \$a0 = i - 1 addi \$sp, \$sp, -1 ! start a new frame jalr \$at, \$ra ! calls factorial(i - 1) lw \$t2, 0(\$sp) ! \$t2 = factorial(i - 1) addi \$sp, \$sp, 2 ! pop return value and argument from stack lw \$t0, 4(\$fp) ! load argument from run-time stack addi \$t1, \$zero, 0 ! \$t1 is the counter addi \$v0, \$zero, 0 ! initialize \$v0 as 0 mult: beq \$t1, \$t2, exit ! branch to exit if equal add \$v0, \$v0, \$t0 addi \$t1, \$t1, 1 beq \$zero, \$zero, mult base: addi \$v0, \$zero, 1 ! \$v0 stores 1 sw \$v0, 0(\$fp) ! store result in memory for fn return: lw \$v0, 0(\$fp) ! load return value from fn sw \$v0, 3(\$fp) ! save return value lw \$ra, 2(\$fp) ! restore return address addi \$sp, \$fp, 3 ! stack pointer points to return value lw \$fp, 1(\$fp) ! restore frame pointer jalr \$ra, \$zero ! return exit: sw \$v0, 0(\$fp) ! store result in memory for fn beq \$zero, \$zero, return stack: .word 0x4000 ! the stack begins here (for example, that is) `; // 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