Guides

Regex cheat sheet for practical JavaScript patterns

Common JavaScript regular expression patterns, flags, and matching notes for day-to-day development.

Choose the JavaScript engine on purpose

Regex syntax changes across engines, so the first quality check is confirming the pattern will run in JavaScript. A pattern copied from PCRE, Python, or a search product can fail on lookbehind support, named groups, escaping, or flag behavior.

  • Test with the browser JavaScript RegExp engine before copying.
  • Keep one sample that must match and one sample that must fail.
  • Check flags separately from the pattern body.

Use common snippets as drafts

Email, URL, UUID, IPv4, HEX color, ISO date, and slug snippets are useful starting points, but none of them can prove business validity by themselves. Treat each snippet as a readable draft that still needs product-specific boundary checks.

  • Use email regex for shape checks, not deliverability.
  • Use URL regex for extraction only when URL parsing is not available.
  • Use UUID and IPv4 snippets to find candidates in logs before validation.

Inspect capture groups before using parsed values

A regex can match the right text while returning the wrong captured value to application code. Inspecting group output makes route parsing, log extraction, and validation code easier to review before the pattern is committed.

  • Name groups when the target runtime supports them.
  • Prefer non-capturing groups when a group is only for precedence.
  • Compare group positions after adding optional parts.

Avoid fragile escaping mistakes

Most production regex bugs come from copying the pattern into a string literal, JSON value, route config, or shell command without adjusting backslashes. Check the raw pattern and the slash-wrapped or string-literal form separately.

  • A JavaScript string often needs doubled backslashes.
  • A JSON string needs escaped quotes and backslashes.
  • A slash-wrapped literal needs escaped slashes inside the pattern.

Continue the workflow with adjacent tools

Regex checks usually sit inside a larger debugging loop. Format JSON logs before extracting fields, parse URLs before writing URL patterns, and use Text Diff when a pattern changed between releases.

  • JSON Formatter helps expose payload fields before pattern matching.
  • URL Encoder protects callback and redirect parameters.
  • Text Diff makes pattern revisions easier to review.

Before copying

A short review loop for safer reuse

Regex Tester

Test JavaScript regular expressions, inspect matches, and keep common patterns close by.

Code

Use cases

  • Validate JavaScript regular expressions
  • Inspect capture groups and match positions
  • Apply common snippets for email, URL, ISO date, UUID, IPv4, HEX color, and slug patterns
  • Test practical form, log, and route patterns

Common failure cases

  • A pattern works in PCRE or Python but fails in the JavaScript RegExp engine.
  • Backslashes are copied from a string literal without escaping them for the target runtime.
  • The global flag changes repeated test behavior through lastIndex state.

Before copying

  • Run one positive sample and one negative sample before copying the pattern.
  • Confirm each flag is intentional: g, i, m, s, u, or y.
  • Copy either the raw pattern or the slash-wrapped form based on the target API.

Examples

Email shape

Good for simple form validation previews.

^[^\s@]+@[^\s@]+\.[^\s@]+$

URL path

Checks slug-style application routes.

^/tools/[a-z0-9-]+$

Capture date

Shows capture groups for ISO-like dates.

(\d{4})-(\d{2})-(\d{2})

FAQ

Which regex engine is used?

The tester uses the browser JavaScript RegExp engine, so behavior matches modern web applications.

Why does a valid-looking regex fail?

JavaScript needs escaped backslashes and supports a specific flag set. Check the error panel for syntax details.

Can this generate a final production regex?

Use generated or preset patterns as a draft, then test positive and negative samples in the JavaScript runtime that will use the pattern.

JSON Formatter

Format, minify, validate, inspect, and copy a safe API response report for JSON payloads without sending them to a server.

Data

Use cases

  • Format compact API responses before copying them into code or docs
  • Copy an API response report with structure, diagnostics, useful JSON paths, and safe sharing checks
  • Turn a DevTools Network response body into a redacted Markdown handoff report
  • Validate JSON syntax and locate parse errors quickly

Common failure cases

  • Trailing commas, comments, or single quotes make browser JSON parsing fail.
  • A huge pasted response contains secrets, tokens, or customer rows that should be redacted first.
  • Dates, IDs, and large numbers can look valid but still be wrong for the downstream schema.

Before copying

  • Validate first, then choose formatted or minified output for the next tool.
  • Remove bearer tokens, cookies, customer IDs, and private endpoint values.
  • Compare the output shape with the API contract before copying it into code or docs.

Examples

Common input

Paste compact API payloads to format, inspect, or turn them into a response report.

{"status":"ok","items":[1,2,3]}

Typical output

Use this as a quick sanity check before copying results.

{ "status": "ok" }

API error response

Useful for formatting copied API failures and copying a redacted response report.

{"error":{"code":"invalid_request","message":"Missing id"},"requestId":"req_123"}

FAQ

Does JSON Formatter upload my input?

No. This tool runs in your browser unless the privacy badge explicitly says a server route is required.

Can I use this for production secrets?

Avoid pasting sensitive production data into any website. Prefer local test data or redacted payloads.

Why does pasted JSON fail even when it looks close?

Common causes are trailing commas, comments, single quotes, unescaped newlines, and copied log prefixes before the JSON value.

URL Encoder

Encode and decode URL components for query strings, redirects, and callbacks.

Web

Use cases

  • Encode values before nesting them inside query strings.
  • URL Encoder for web workflows

Common failure cases

  • URL Encoder can still fail when the pasted input shape differs from https://example.com/callback?state=a b.
  • The output should be reviewed in the target web workflow before reuse.
  • Browser-local processing does not make sensitive production data safe to paste.

Before copying

  • Compare the output against the original input before copying.
  • Remove secrets, customer data, and one-off environment values.
  • Continue with url-parser if the result needs another validation step.

Examples

Common input

Encode values before nesting them inside query strings.

https://example.com/callback?state=a b

Typical output

Use this as a quick sanity check before copying results.

https%3A%2F%2Fexample.com%2Fcallback%3Fstate%3Da%20b

FAQ

Does URL Encoder upload my input?

No. This tool runs in your browser unless the privacy badge explicitly says a server route is required.

Can I use this for production secrets?

Avoid pasting sensitive production data into any website. Prefer local test data or redacted payloads.

Regex cheat sheet for practical JavaScript patterns | bobob.app