The snippet library.
Small, practical examples. Search by task or narrow things down below.
Build a URL query string from values
JavaScript: URLSearchParams handles query escaping and repeated parameter appends.
JavaScript✓ Tested
Decode a percent-encoded URL component
JavaScript: decodeURIComponent reverses percent encoding and throws for malformed sequences.
JavaScript✓ Tested
Extract all regex matches with their groups
JavaScript: matchAll requires a global regular expression and returns an iterator of matches.
JavaScript✓ Tested
Inspect the parsed pieces of a URL
JavaScript: Use URL properties rather than splitting a URL at punctuation.
JavaScript✓ Tested
Percent-encode one URL component
JavaScript: encodeURIComponent escapes a parameter value rather than a complete URL.
JavaScript✓ Tested
Read named regex capture groups
JavaScript: Named groups make a fixed input format easier to inspect than numeric group positions.
JavaScript✓ Tested
Reorder captured text in a replacement
JavaScript: Use numbered replacement groups to rearrange a known two-part name.
JavaScript✓ Tested
Resolve a relative URL against a base
JavaScript: A trailing slash makes the final base path component a directory.
JavaScript✓ Tested
Split text on several separator characters
JavaScript: A regex character class can recognize alternative simple delimiters.
JavaScript✓ Tested
Validate a whole ASCII identifier
JavaScript: Use an anchored expression without the multiline flag and reject a trailing line break explicitly.
JavaScript✓ Tested