JavaScript Regex, JSON, and Date Cheat Sheet

Regular expressions, JSON parsing and serialization, dates, Intl formatting, math, and crypto helpers.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Regular Expressions
RegExp test
const isHex = /^[0-9a-f]+$/i.test(value);
String match
const matches = text.match(/\d+/g);
String matchAll
const all = [...text.matchAll(/(?<year>\d{4})/g)];
Named capture groups
const m = /(?<user>[a-z]+)@(?<host>.+)/i.exec(email);
const host = m?.groups?.host;
Regex replace
const normalized = text.replace(/\s+/g, " ").trim();
Regex split
const parts = csvLine.split(/\s*,\s*/);
Escape regex special chars
function escapeRegex(text) {
  return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
## JSON
JSON.parse
const data = JSON.parse(raw);
JSON.stringify
const json = JSON.stringify(data);
Pretty-print JSON
const pretty = JSON.stringify(data, null, 2);
JSON.parse reviver
const parsed = JSON.parse(raw, (key, value) => key.endsWith("At") ? new Date(value) : value);
JSON.stringify replacer
const json = JSON.stringify(data, (key, value) => key === "password" ? undefined : value);
## Dates and Time
Date.now
const timestamp = Date.now();
Create Date
const date = new Date("2026-03-06T12:00:00Z");
toISOString
const iso = new Date().toISOString();
Intl.DateTimeFormat
const fmt = new Intl.DateTimeFormat("en-US", { dateStyle: "medium", timeStyle: "short" });
fmt.format(new Date());
Intl.NumberFormat
const nf = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" });
nf.format(1999.5);
Intl.RelativeTimeFormat
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
rtf.format(-1, "day");
Add days
const nextWeek = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000);
## Math and Random
Random integer
const n = Math.floor(Math.random() * 10);
Round / floor / ceil
Math.round(1.6);
Math.floor(1.6);
Math.ceil(1.2);
Clamp helper
const clamp = (n, min, max) => Math.min(Math.max(n, min), max);
crypto.randomUUID
const id = crypto.randomUUID();
crypto.getRandomValues
const bytes = crypto.getRandomValues(new Uint8Array(16));
SHA-256 digest
const data = new TextEncoder().encode("hello");
const hash = await crypto.subtle.digest("SHA-256", data);