Regex Empty String: How to Match It or Allow It
How to match an empty string with a regular expression, make an existing pattern also accept empty input, and test the result without letting unintended text through.
An empty string is text with zero characters, written in most languages as two quotes with nothing between them: "". Regular expressions handle it in two common situations. You may want to detect input that is completely empty, or you may have a validation pattern that should also accept a blank field. This guide covers both, explains the anchors that make them work, and shows how to test the pattern so it does not accept spaces or other text you meant to reject.
What an empty-string regex does to your text
An empty-string pattern does not change your text. It is a test. You give it an input string, and it reports whether the whole input has zero characters. When you make a pattern allow empty input, the pattern still accepts the same values it accepted before and also accepts "". Its scope is the entire input, not a word or a line inside it, because anchors pin the match to the start and end.
Two terms come up repeatedly. An anchor matches a position rather than a character. ^ marks the start of the input, and $ marks the end. A quantifier controls how many times something may repeat. ? means zero or one, * means zero or more, and + means one or more. Empty-string handling depends on these two ideas.
Matching only an empty string
The basic pattern puts the start and end anchors next to each other, with nothing between them:
^$
This matches when the start of the input is immediately followed by its end, meaning there are no characters. It does not match a single space. A space is a real character, even though you cannot see it.
Leaving out the anchors is a common mistake. An empty pattern, or a pattern such as x* that can match zero characters, succeeds against any input, because the empty string appears at every position in every string. Without ^ and $, a check like that always reports a match.
Engine differences at the end of input
Regex engines disagree about what $ means near a trailing newline. This matters if your input might end with a line break.
| Engine or language | Does ^$ match "\n" (a single newline)? | Strict end-of-input anchor |
| JavaScript (without the m flag) | No, $ matches only at the very end | No \z; use ^$ without the m flag |
| Python re | Yes, $ also matches just before a final newline | \Z (absolute end in Python) |
| PCRE, Java, Ruby | Yes, $ also matches before a final newline | \z |
For a strict "truly empty" check in Python, use \A\Z, or call re.fullmatch with an empty pattern. In PCRE, Java and Ruby, use \A\z. This table reflects the general documented behavior of these engines, but flags and versions can change it. Check your engine's documentation for edge cases it does not cover. The supplied material contained no engine-specific evidence.
Multiline mode changes the meaning
With the multiline flag enabled (m in JavaScript, PCRE and Java, re.MULTILINE in Python), ^ and $ match at the start and end of every line. Under that flag, ^$ finds blank lines inside a larger text rather than testing whether the whole input is empty. Finding blank lines is useful when cleaning up a document. If you are validating a single field, though, turn the flag off.
Allowing an empty string in an existing pattern
Suppose you already have a pattern that validates a value, and a blank field should now pass too. There are two standard approaches.
Option 1: make the whole pattern optional
Wrap the existing pattern in a non-capturing group, (?: ... ), and add ? after it. The group then appears zero times, which gives the empty string, or exactly once, which gives a valid value. The anchors stay outside the group.
Before: ^\d{5}$
After: ^(?:\d{5})?$
Example (illustrative): the first pattern accepts exactly five digits, such as a basic US ZIP code. The second accepts five digits or nothing at all. This is the approach to use in most cases.
Option 2: use alternation
Alternation uses | to mean "either this or that." You can list the empty case explicitly:
^(?:\d{5}|)$
The empty branch after | matches nothing, so the input may be empty. Keep the anchors outside the group. In a pattern such as ^\d{5}|$, the anchors bind to the separate alternatives, and the $ branch matches the end of any input. That turns the pattern into one that accepts everything.
Option 3: change + to * when the pattern repeats one thing
If the pattern is a single character class repeated, such as ^[a-z]+$, switching + (one or more) to * (zero or more) also allows empty input:
^[a-z]*$
Use this only when the whole pattern is one repeated element. For structured patterns, such as a fixed count like {5} or several parts in sequence, wrap the pattern and make it optional instead. Changing one quantifier inside a structured pattern can let through partial values you meant to reject.
Empty versus whitespace-only input
Decide whether a field that contains only spaces, tabs or line breaks counts as empty. The patterns above reject such input, because those characters are real content. To treat whitespace-only input as blank, use \s*, where \s matches whitespace characters:
Empty or whitespace only: ^\s*$
Valid value, empty, or spaces: ^(?:\d{5}|\s*)$
Which characters \s covers depends on the engine and its Unicode settings. Non-breaking spaces and other Unicode spaces may or may not be included. If you need exact control, list the allowed characters explicitly, for example [ \t]* for spaces and tabs only. Another option is to trim the input in code before running the regex.
Step by step: allow an empty string and verify it
- 1. Write down the pattern you have now, and confirm it already has ^ at the start and $ at the end (or \A and \z, depending on the engine).
- 2. Decide whether whitespace-only input should count as empty.
- 3. Wrap the existing pattern as ^(?:existing)?$, keeping the anchors outside the group. If whitespace should also pass, use ^(?:existing|\s*)$.
- 4. Turn off the multiline flag if you are validating a single value.
- 5. Paste the pattern into a regex tester set to the same engine or language your code uses.
- 6. Run the test inputs below and compare each result to what you intended.
- 7. Test in your actual code as well, because some testers default to a different engine than the one your application uses.
Example test set for ^(?:\d{5})?$. These are the intended results, not verified tool output. Invisible characters are written out in brackets.
| Input | Intended result | Why |
| "" (nothing) | Match | The optional group appears zero times |
| "12345" | Match | The original valid value |
| "1234" | No match | Too few digits |
| "123456" | No match | Too many digits |
| " " [one space] | No match | A space is a character |
| "12345\n" [trailing newline] | Varies by engine | $ may match before a final newline; use \z or \Z for strict checks |
If a value that should fail is accepted, the usual causes are missing anchors, anchors placed inside an alternation, or the multiline flag being on.
Side effects in search and replace
Patterns that can match zero characters behave unexpectedly in global find-and-replace and in split operations. Each empty match happens at a position between characters, so a replacement inserts text at every one of those positions. In JavaScript, for example, "abc".replace(/x*/g, "-") produces "-a-b-c-", because x* matches the empty string at each of the four positions. If your goal is to transform text rather than validate it, make sure the pattern requires at least one character (use + rather than *). Then preview the replacement before applying it, so meaningful content is not altered.
HTML form validation note
If the regex lives in an HTML input's pattern attribute, you usually do not need to allow empty input yourself. Under the HTML specification, browsers skip the pattern check when the field is empty. The pattern applies only once the user types something. To make a field mandatory, add the required attribute. Leave required off and the field accepts empty input, even with a strict pattern.