Home / Guides / Regex Find and Replace: Practical Examples

Regex Find and Replace: Practical Examples

Use practical JavaScript regex patterns for numbers, whitespace, line anchors, capture groups, IDs, and dates.

How to use the examples

Enter the pattern without slash delimiters in Regex Find & Replace. Choose flags separately. Test on a short copy of your data before applying a broad pattern.

Find numbers

Pattern: \d+
Input: Order 123, Order 987
Replacement: NUMBER
Output: Order NUMBER, Order NUMBER

Normalize whitespace

Pattern: [ \t]+
Replacement: one space

This targets spaces and tabs without consuming line breaks. By contrast, \s+ can include line breaks.

Match the beginning of each line

Pattern: ^ID:
Replacement: Item:
Flag: m

Multiline mode makes ^ apply after each line break. Use $ for line endings.

Reuse capture groups

Pattern: Order (\d+)
Replacement: ID-$1
Input: Order 123
Output: ID-123

Restructure an identifier

Pattern: ([A-Z]{3})-(\d{4})
Replacement: $2/$1
Input: ABC-2026
Output: 2026/ABC

Reformat a date string

Pattern: (\d{4})-(\d{2})-(\d{2})
Replacement: $3.$2.$1

This rearranges matching text but does not validate calendar dates. Use a dedicated date parser when validation matters.

Remove repeated markers

Pattern: (?:TODO:\s*)+
Replacement: TODO: 

Before replacing

Related guides

Browse all TextUnicorn guides →