Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 38 ยท Syntax and Matching

Literals, Escaping and the Dot

Learn how literal characters match themselves, which characters are special in a regex, how to escape them with a backslash, and how the dot wildcard behaves with newlines and the s flag.

The simplest regex is plain text: /cat/ matches the three characters c, a, t, in that order, anywhere in the string. Letters, digits and most punctuation are literals that match themselves. The interesting part begins with the characters that are not literal: the metacharacters.

Literal matching

Node.js Plain text is a pattern

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

By default matching is case-sensitive and looks at every position, so cat is found inside concatenate. Whitespace is literal too: a space in the pattern needs a space in the text.

The special characters

These characters have a special meaning outside a character class:

TEXT
.  *  +  ?  ^  $  (  )  [  ]  {  }  |  \  /

(The / is special only in a regex literal, where it ends the pattern.) If you want one of them as plain text, put a backslash in front of it. That is called escaping.

Node.js Escaping metacharacters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

In the first line the pattern 1+1 means "one or more 1s, then another 1", so it needs the text 11. The string 1+1=2 contains no 11, so the result is null. That is exactly the kind of surprise escaping prevents.

The backslash is itself special. To match a literal backslash you need \\ in a regex literal, and '\\\\' in a new RegExp string (one level for the string, one for the regex).

Escaping user input

When the pattern comes from user input (a search box), never put the raw text into new RegExp: characters such as ( or * would be interpreted. Escape first.

Node.js A reusable escape function

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The class [.*+?^${}()|[\]\\] lists every special character; $& in the replacement means "the matched character", so each one gets a backslash in front. (Recent engines also provide RegExp.escape(), but the function above works everywhere.)

The dot

The dot . matches any single character except a line terminator (\n, \r, \u2028, \u2029).

Node.js What the dot matches

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Two details matter:

  • The dot does not cross line breaks. To make it match newlines too, use the s flag (also called "dotAll").
  • Without the u flag, the dot sees UTF-16 code units, so an emoji counts as two. Use u to treat it as one character.
Node.js The s flag and the [\s\S] trick

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Overusing the dot

The dot is convenient and dangerous. .* means "anything, any amount", which hides intent and can match far more than you want. Prefer a precise class when you know what is allowed.

Node.js Dot versus a precise class

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which pattern matches the literal text 3.5 and nothing like 3x5?

    An unescaped dot is a wildcard. \. (or [.]) matches a real dot.
  2. 2. What does the dot not match by default?

    The dot matches any character except line terminators, unless you add the s flag.
  3. 3. How do you write a regex literal that matches one backslash?

    Each backslash escapes the next, so two backslashes in the literal match one backslash in the text.
  4. 4. Why escape user input before passing it to new RegExp?

    Unescaped metacharacters change the meaning of the pattern or throw a SyntaxError.

Summary

  • Letters and digits are literals; matching is case-sensitive by default.
  • Escape . * + ? ^ $ ( ) [ ] { } | \ / with a backslash to match them literally.
  • Escape user input with a helper before building a regex from it.
  • The dot matches any character except line breaks; the s flag lets it match newlines, and u makes it count code points.

Next: Anchors and Word Boundaries