Introduction
Regular expressions can model simple substitution ciphers and text transformations in a single replace operation. This tool lets you test regex-based cipher patterns against sample text, with a library of predefined transformations including ROT13, Atbash, Caesar shift, vowel removal, text reversal, alternating case, and Pig Latin. You can also write custom regex patterns with replacement strings and toggle the g, i, and m flags. The before-and-after output updates as you type.
What this tool does
- Apply predefined cipher transformations (ROT13, Atbash, Caesar shift by N, vowel removal, reverse text, alternating case, Pig Latin) using dedicated transform functions for correct output
- Accept custom regex patterns with replacement strings, supporting backreferences ($&, $1, $2) and the g (global), i (case-insensitive), and m (multiline) flags
- Display before-and-after output side by side so you can see exactly what changed
- Provide a Caesar shift slider (-25 to +25) for the Caesar shift by N pattern, updating the output in real time
- Show the regex pattern and description for library transformations so you understand how each one works
- Report regex syntax errors immediately with the specific error message from the JavaScript RegExp engine
How this tool works
The tool has two modes. In library mode, selecting a predefined pattern from the dropdown applies a dedicated transform function rather than a raw regex replacement. This is necessary because some transformations (ROT13, Atbash, Caesar, reverse, alternating case) require per-character logic that cannot be expressed in a regex replacement string alone.
For example, ROT13 shifts each letter by 13 positions. The library function iterates over each character, checks if it is a letter, computes `(charCode - base + 13) % 26 + base`, and builds the result. The regex pattern `[a-zA-Z]` is shown for reference, but the actual transform uses the function.
In custom mode, the tool constructs a `new RegExp(pattern, flags)` from the user-supplied pattern and flags, then calls `input.replace(re, replacement)`. This supports standard JavaScript replacement string syntax including `$&` (matched substring), `$1` through `$9` (capture groups), and literal text.
The Caesar shift by N pattern includes a numeric input field. When you change the shift value, the tool re-applies the transform with the new N. The formula is `((charCode - base + N) % 26 + 26) % 26 + base`, where the double modulo handles negative shifts correctly.
How regex cipher testing works (ECMA-262 RegExp)
Regular expressions in JavaScript follow the ECMA-262 specification, which defines the `RegExp` constructor and the `String.prototype.replace` method. The MDN regex documentation provides a practical reference for the syntax and available flags.
The g flag enables global replacement (all matches, not just the first). The i flag makes matching case-insensitive. The m flag changes the behavior of `^` and `$` to match line boundaries instead of string boundaries. These three flags cover most text transformation use cases.
Regex-based cipher testing is useful for understanding how substitution patterns work. ROT13 is a special case of the Caesar cipher where the shift is 13, making it its own inverse (applying ROT13 twice returns the original text). Atbash maps each letter to its mirror position (A to Z, B to Y, C to X), which is also self-inverse. The Caesar cipher with a shift of N is not self-inverse unless N is 13.
For full Caesar cipher encoding and decoding with brute-force analysis, see the Caesar Cipher tool. For Atbash, see the Atbash Cipher. For ROT13 specifically, use the ROT13 tool. For escaping special regex characters in patterns, see the Regex Escaper.
How to use this tool
- Enter or paste sample text into the input field
- Select a predefined pattern from the Pattern library dropdown, or choose 'Custom pattern' to write your own regex
- For the Caesar shift by N pattern, adjust the shift value using the numeric input (-25 to +25)
- For custom patterns, enter your regex in the Regex pattern field and a replacement string in the Replacement field
- Toggle the g, i, and m flags as needed for your pattern
- Review the before-and-after output to see the transformation result, and check for any regex syntax errors
Real-world examples
Applying ROT13 to encode a message
Input: `Hello World`. Select ROT13 from the library. The output is `Uryyb Jbeyq`. Each letter is shifted by 13 positions: H becomes U, e becomes r, l becomes y, and so on. Applying ROT13 again to `Uryyb Jbeyq` returns `Hello World`, since 13 + 13 = 26 (a full rotation).
Caesar shift with a custom rotation value
Input: `Attack at dawn`. Select 'Caesar shift by N' and set N to 5. The output is `Fyyfhp fy ifbs`. Each letter shifts forward by 5: A becomes F, t becomes y, c becomes h. To decrypt, set N to -5 (or 21, since -5 mod 26 = 21).
Custom regex for extracting initials
Select 'Custom pattern'. Enter `\b(\w)\w+` in the pattern field and `$1.` in the replacement field with the g flag. Input: `John Ronald Reuel Tolkien`. Output: `J. R. R. T.`. The regex captures the first letter of each word and discards the rest, appending a period.
Atbash cipher for a CTF challenge
Input: `ZMXPVH GSRH HVXIVN`. Select Atbash from the library. The output is `ANCKES TITH SECREM`. Atbash reverses the alphabet: Z maps to A, M maps to N, X maps to C. This is a common warmup challenge in CTF competitions. For a full Atbash tool with encode and decode modes, see the Atbash Cipher.
Comparison with similar methods
| Method | Complexity | Typical use |
|---|---|---|
| ROT13 | Shift by 13, self-inverse | Spoiler hiding, puzzle hints, CTF warmups |
| Caesar shift by N | Shift by N, 25 possible keys | Introduction to substitution ciphers |
| Atbash | Mirror alphabet, self-inverse | Puzzle challenges, historical cipher study |
| Custom regex replace | Arbitrary pattern and replacement | Text transformation, data extraction |
| Pig Latin | Consonant cluster move + 'ay' | Linguistic play, text obfuscation games |
Limitations or considerations
Library transformations use dedicated functions rather than raw regex replacement strings. This is because JavaScript's `String.prototype.replace` with a string replacement cannot execute per-character logic like modular arithmetic. The regex pattern is shown for reference but the actual computation uses a function.
Custom regex mode uses the JavaScript `RegExp` constructor, which follows ECMA-262 syntax. Some regex features available in other engines (like PCRE's lookbehind before ES2018, or named backreferences with `\k
This tool is for testing and learning. It is not a full cipher implementation. For complete Caesar cipher with brute-force analysis, use the Caesar Cipher tool. For ROT13, use the ROT13 tool.
The tool does not handle Unicode categories like `\p{L}` (all letters) unless the u flag is enabled, which this tool does not expose. For Unicode-aware text processing, consider a dedicated Unicode tool.
Frequently asked questions
Why do library patterns use functions instead of regex replacement strings?
JavaScript regex replacement strings support backreferences like $& and $1, but they cannot execute arbitrary logic per match. ROT13 needs to compute (charCode - base + 13) % 26 for each letter, which requires a function. The tool uses dedicated functions for library patterns to ensure correct output.
What is the difference between ROT13 and a Caesar cipher?
ROT13 is a specific Caesar cipher with a shift of 13. Because 13 is half of 26, applying ROT13 twice returns the original text. A Caesar cipher with any other shift is not self-inverse. ROT13 is commonly used to hide spoilers in online forums.
Can I use capture groups in custom regex mode?
Yes. The tool uses JavaScript's String.prototype.replace, which supports $1 through $9 for capture groups, $& for the full match, and $` / $' for the text before/after the match. For example, pattern `(\w+)@(\w+)` with replacement `$2.$1` swaps the two captured groups.
What regex flags are available?
The tool supports three flags: g (global, replace all matches), i (case-insensitive matching), and m (multiline, where ^ and $ match line boundaries). These are the most common flags for text transformation. The s (dotAll) and u (Unicode) flags are not exposed in this tool.
How does the Atbash cipher work?
Atbash maps each letter to its mirror position in the alphabet. A maps to Z, B maps to Y, C maps to X, and so on. The formula is `base + 25 - (charCode - base)`, where base is 65 for uppercase and 97 for lowercase. Atbash is self-inverse: applying it twice returns the original text.
Conclusion
Regex-based cipher testing is a practical way to understand how simple substitution patterns transform text. This tool combines a library of predefined transformations with custom regex mode for flexible text processing. For full cipher implementations with brute-force analysis, see the Caesar Cipher, Atbash Cipher, and ROT13 tools. For escaping special regex characters, use the Regex Escaper.