Code & computing / QUICK REFERENCE

Regular expressions

JavaScript regular expressions with input and match results, character classes, anchors, groups and common string operations.

JavaScript RegExp. Pattern examples show the first match from a fresh regex.exec(input), plus captures where relevant. “No match” means null. Strings use JavaScript escapes, so \n is a newline. The four operation examples describe their own results.

30 entries

Characters and classes

Literal sequence
/atlas/

Looks for that exact sequence. [2][1][5]

  • Input"atlas"
    ResultMatch "atlas"
Any ordinary character
/./

Excludes line terminators unless dotAll is enabled. [2][1][5]

  • Input"a"
    ResultMatch "a"
  • Input"\n"
    ResultNo match
ASCII digit
/\d/

Matches 0 through 9 in JavaScript. [2][1][5]

  • Input"a1"
    ResultMatch "1"
  • Input"٣"
    ResultNo match
Nondigit
/\D/

Complements the digit class. [2][1][5]

  • Input"A"
    ResultMatch "A"
Word character
/\w/

Primarily ASCII letters, digits and underscore; not a universal alphabet class. [2][1][5]

  • Input"_"
    ResultMatch "_"
  • Input"é"
    ResultNo match
Whitespace
/\s/

Includes more than an ordinary space. [2][1][5]

  • Input"\t"
    ResultMatch "\t"
One of these characters
/[abc]/

Matches exactly one listed character. [2][1][5]

  • Input"b"
    ResultMatch "b"
Not one of these
/[^abc]/

Negates the character class. [2][1][5]

  • Input"d"
    ResultMatch "d"
ASCII lowercase range
/[a-z]/

Does not cover accented letters. [2][1][5]

  • Input"m"
    ResultMatch "m"
Literal dot
/\./

Escapes the dot's special meaning. [2][1][5]

  • Input"."
    ResultMatch "."

Repetition and anchors

Zero or more
/ab*/

Repeats b, not the whole ab sequence. [3][1][5]

  • Input"a"
    ResultMatch "a"
One or more
/ab+/

Requires at least one b. [3][1][5]

  • Input"abbb"
    ResultMatch "abbb"
Optional character
/colou?r/

Accepts color or colour. [3][1][5]

  • Input"color"
    ResultMatch "color"
Exact count
/\d{4}/

Requires four consecutive digits, not necessarily the whole input. [3][1][5]

  • Input"a12345"
    ResultMatch "1234"
  • Input"123"
    ResultNo match
Bounded count
/\d{2,4}/

Matches between two and four digits. [3][1][5]

  • Input"12345"
    ResultMatch "1234"
Lazy repetition
/a.*?b/

Chooses the shortest successful expansion. On "a12bZZb", this matches "a12b"; the greedy /a.*b/ matches "a12bZZb". [3][1][5][8]

  • Input"a12bZZb"
    ResultMatch "a12b"
  • Input"a12c"
    ResultNo match
Start anchor
/^atlas/

Requires the match at input start without the m flag. [3][1][5][6]

  • Input"atlas is first"
    ResultMatch "atlas"
  • Input"my atlas"
    ResultNo match
End anchor
/atlas$/

Without m, the match must end at the end of the input: a trailing newline prevents this match. With m, $ can also match just before a line terminator. [3][1][5][6]

  • Input"my atlas"
    ResultMatch "atlas"
  • Input"atlas\n"
    ResultNo match
Word boundary
/\bcat\b/

Boundaries follow the engine's word-character rules. [3][1][5]

  • Input"a cat!"
    ResultMatch "cat"
  • Input"catalog"
    ResultNo match
Whole ASCII digits
/^\d+$/

A simple nonnegative-integer shape, not a complete numeric parser. [3][1][5]

  • Input"123"
    ResultMatch "123"
  • Input""
    ResultNo match

Groups and common operations

Alternation
/cat|dog/

Either alternative may match. [4][1][5]

  • Input"cat"
    ResultMatch "cat"
Capture a group
/(ab)+/

The full match can repeat ab; group 1 holds the last captured iteration. [4][1][5]

  • Input"abab"
    ResultMatch "abab"; group 1 = "ab"
Group without capturing
/(?:ab)+/

Groups repetition without creating a capture slot. [4][1][5]

  • Input"abab"
    ResultMatch "abab"
Named capture
/(?<year>\d{4})/

Reads the capture through groups.year after a successful match. [4][1][5]

  • Input"in 2026"
    ResultMatch "2026"; groups.year = "2026"
Repeated word
/\b(\w+)\s+\1\b/

Backreference repeats the captured characters. [4][1][5]

  • Input"word word"
    ResultMatch "word word"; group 1 = "word"
Ignore case
/atlas/i

Case behavior depends on Unicode and engine rules. [4][1][5]

  • Input"ATLAS"
    ResultMatch "ATLAS"
  • Input"other"
    ResultNo match
Find all matches
[...'a1 b2'.matchAll(/\d/g)]

matchAll requires a global regex argument. The spread produces match arrays; the result below summarizes their text and zero-based indices. [4][1][9]

  • Input"a1 b2"
    ResultMatches "1" (index 1), "2" (index 4)
Test for a match
/\d/.test('item 2')

Returns a boolean. [4][1]

  • Input"item 2"
    Resulttrue
Replace matches
'a1 b2'.replace(/\d/g, '#')

Produces a# b#. [4][1]

  • Input"a1 b2"
    Result"a# b#"
Split on comma spacing
'a, b,c'.split(/,\s*/)

Produces three text fields for this simple input. [4][1]

  • Input"a, b,c"
    Result["a","b","c"]

Reserved placement, separate from reference controls. No ad network is active.

Flags and repeated tests

m: line boundaries

The multiline flag lets ^ and $ match line boundaries as well as input boundaries. It does not make a dot match a newline.

/atlas$/.test("atlas\n")  // false
/atlas$/m.test("atlas\n") // true
/a.b/m.test("a\nb")      // false
Input boundary rules ↓

g: keep track of the next match

Repeated calls on the same global regex use lastIndex. A failed search resets it to zero. Sticky y regexes also carry state; use a fresh regex for independent checks.

const re = /\d/g;
re.test("1 2"); // true;  lastIndex = 1
re.test("1 2"); // true;  lastIndex = 3
re.test("1 2"); // false; lastIndex = 0
lastIndex rules ↓
Updated JavaScript RegExp · common modern features

Sources

  1. Regular expressions guide ↗
  2. Character classes ↗
  3. Assertions ↗
  4. Groups and backreferences ↗
  5. RegExp.exec() results and captures ↗
  6. Input boundary assertions: ^ and $ ↗
  7. RegExp.lastIndex and repeated matches ↗
  8. Greedy and lazy quantifiers ↗
  9. String.matchAll() match arrays ↗