Handy “Regex Snippets” I Use All the Time in Solo Development

When working on personal projects, I often find myself repeatedly looking up the same regular expressions—email validation, URLs, dates, digits, whitespace removal, and more.

Here’s a collection of practical, copy-and-paste–ready regex snippets that frequently appear across projects.
The examples use JavaScript/TypeScript, but almost all of them work in any mainstream language.

1. Email validation (practical version)

Not fully RFC-complete—just the most usable option for real-world apps.

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

Example:

const isEmail = (value: string) =>
  /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);

2. URL validation (http/https)

^https?:\/\/[^\s/$.?#].[^\s]*$

A balanced pattern that works well in most applications without being overly strict.

3. Digits only (integer)

^\d+$

Allow negative and floating-point numbers:

Negative + decimal allowed

^-?\d+(\.\d+)?$

4. Remove all whitespace

\s+

Usage:

value.replace(/\s+/g, "");

5. Working with comma-formatted numbers

Remove all commas

,/g

Insert commas every 3 digits

value.replace(/\B(?=(\d{3})+(?!\d))/g, ",");

6. Basic phone number check (Japan)

If you work with Japanese phone numbers:

^0\d{1,4}-?\d{1,4}-?\d{3,4}$

Flexible and practical for most use cases.

7. Alphanumeric only

^[A-Za-z0-9]+$

Useful for user IDs, passwords, etc.

8. Alphanumeric + specific safe symbols

^[A-Za-z0-9._-]+$

Common for filenames, slugs, and usernames.

9. Japanese characters only (Hiragana, Katakana, Kanji)

^[\p{Hiragana}\p{Katakana}\p{Han}]+$

JavaScript requires the u flag:

/^[\p{Hiragana}\p{Katakana}\p{Han}]+$/u

10. ISO8601 date (YYYY-MM-DD)

^\d{4}-\d{2}-\d{2}$

With time:

YYYY-MM-DDTHH:MM:SSZ

^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$

11. Extract file extension

\.([0-9A-Za-z]+)$

Example:

const ext = filename.match(/\.([0-9A-Za-z]+)$/)?.[1];

12. Remove simple HTML tags

Not perfect for complex HTML, but great for lightweight cleanup.

<[^>]+>
text.replace(/<[^>]+>/g, "");

13. Collapse multiple newlines into one

\n{2,}

Usage:

text.replace(/\n{2,}/g, "\n");

14. Trim whitespace from start/end (regex version)

^\s+|\s+$

Useful if you’re working inside a pipeline where trim() isn’t suitable.

15. Extract Markdown links

\[(.*?)\]\((.*?)\)

Extracts both the link text and URL.

Summary

These regex snippets cover the common cases you’ll see again and again in personal projects:

  • Validation (email, URL, numbers, alphanumerics)
  • Text cleaning (whitespace, newlines)
  • Parsing (extensions, Markdown, HTML)

Copied title and URL