Escape a dot in a regular expression
How to escape a dot in a regex so it matches a literal period, how the escape changes across languages, shells and JSON, and when a plain-text search is the better choice.
In a regular expression, an unescaped dot is a wildcard. To match an actual period, such as the one in a file extension, a version number or a domain name, you escape it with a backslash and write . instead of a bare dot. This review covers what that escape changes, when it is the right tool, and where it tends to break as a pattern moves between code, terminals, config files and documentation.
Is escaping the dot the right approach for your task?
Escape the dot when you need regex features elsewhere in the same pattern, such as anchors, alternation, character classes or capture groups, and one or more of the characters must be a literal period. The benefit is precision. The pattern stops matching unintended text, so a.b no longer matches 'axb' or 'a-b'.
The main tradeoff is readability. Every escape adds a character, and a single escape often becomes two or more once the pattern sits inside a string literal, a shell command or a JSON file. Those layers are where most mistakes happen.
Plain text is often the better choice. If all you need is to check whether a string contains a fixed substring with a period in it, a non-regex operation avoids escaping entirely. Examples include a contains or indexOf check in code, a plain string replace, or grep -F for fixed-string search in a terminal. Reach for a regex only when the matching logic actually needs one.
What the escape actually changes
Escaping is a change to the stored characters of the pattern. It is not a visual or formatting effect. The pattern text gains a backslash, and the regex engine reads the backslash-plus-dot pair as one literal period.
An unescaped dot matches any single character. In most regex flavors that excludes line breaks by default. A flag usually called dotall or single-line mode, often written as s, makes the dot match newlines too. Escaping removes the wildcard meaning altogether, so these flags no longer matter for that character.
Pattern Input Match?
3.14 3.14 yes
3.14 3514 yes (dot matched '5')
3\.14 3.14 yes
3\.14 3514 no
example.com exampleXcom yes (dot matched 'X')
example\.com exampleXcom no
A character class gives another way to express a literal period. Inside square brackets the dot has no special meaning, so [.] matches only a period. Escaping it there as [.] is redundant but harmless in common engines. Some people prefer [.] in heavily layered contexts because it needs no backslash, which avoids one round of doubling.
The escape matters only in the pattern. In the replacement string of a search-and-replace, a dot is already literal and needs no backslash. Replacement strings have their own special characters, such as $ or \ for group references, depending on the tool.
How the escape is written in different contexts
The regex engine always needs to receive backslash-dot. What you type to get it there depends on whether the pattern first passes through a string-literal parser, a shell or a data format. That parser may consume one backslash before the regex engine sees the pattern.
| Context | What you write | Why |
| JavaScript regex literal | /./ | Regex literals are not string literals, so a single backslash suffices |
| JavaScript RegExp constructor | new RegExp("\.") | The string literal turns \ into one backslash |
| Python raw string | r"." | Raw strings keep the backslash as typed |
| Python ordinary string | "\." | Without r, the backslash must itself be escaped |
| Java string | "\." | Java has no raw regex literal; the string parser consumes one backslash |
| C# verbatim string | @"." | Verbatim strings keep backslashes as typed |
| Go raw string | \. | Backtick strings keep backslashes as typed |
| JSON value | "\." | JSON does not allow . as an escape, so the backslash must be doubled |
| Shell, single-quoted | grep '.' file | Single quotes pass the backslash through unchanged |
The shell case causes many silent failures. If you write grep . file without quotes, the shell removes the backslash, and grep receives a bare dot that matches any character. The command still runs and still returns results, just the wrong ones. Quote regex patterns in the shell. Single quotes are the safest default.
grep '\.log$' files.txt # lines ending in a literal ".log"
grep -F '.log' files.txt # fixed-string search, no regex escaping needed
sed 's/\./,/g' input.txt # replace every literal period with a comma
Escaping a whole literal string automatically
If the text with periods comes from user input or a variable, escaping by hand is error-prone. Most languages provide a function that escapes every regex metacharacter for you:
- Python: re.escape
- Java: Pattern.quote, which wraps the text in \Q...\E rather than escaping each character
- C#: Regex.Escape
- Go: regexp.QuoteMeta
- PHP: preg_quote, which also accepts the pattern delimiter so it can escape that too
- JavaScript: RegExp.escape exists in recent engines but not in older runtimes, so check support in your target environment before relying on it
These functions escape more than the dot, including characters such as *, +, ?, parentheses and brackets. Their exact output can differ between languages. Pass the result to that same language's regex engine rather than copying it elsewhere.
Will the pattern stay correct after copying or sharing?
Escaped patterns are fragile when moved, because each destination may interpret backslashes differently. A pattern copied from a Python raw string into a Java string loses one level of escaping and stops matching what it should. Going the other way, a pattern copied out of a Java string into a regex testing tool carries an extra backslash. When moving a pattern, first work out what the regex engine itself should receive, then re-apply the destination's quoting rules.
Documentation and chat tools can also change the text. In Markdown, a backslash before a punctuation character outside a code span or code block is treated as an escape. Written in ordinary prose, . renders as a plain period and the backslash disappears from what readers see and copy. Put regex patterns in inline code or fenced code blocks when sharing them. Word processors and some messaging apps may also turn straight quotes into curly quotes, which breaks shell commands copied from them.
Readability for people is a separate concern from correctness. Patterns full of backslashes are hard to scan, and doubled escapes in string literals make them harder still. Several practices help. Use raw or verbatim strings where the language offers them. Use [.] when it saves a layer of doubling. Add a short comment or plain-language description of what the pattern matches. A plain description is also the practical fallback for anyone who cannot easily read the pattern, including readers using assistive technology. No tested evidence on how specific screen readers announce backslash-heavy patterns was available for this review, so that behavior is not described here.
Gaps and how to verify
The behavior described here is the generally documented behavior of common regex engines, string-literal rules and shells. It is not the result of tests run for this article. Regex flavors differ in details such as default newline handling, available flags and which escaping helpers exist in a given runtime version. Before relying on a pattern, test it in the exact engine and context where it will run. Include a case that should match and a near-miss that should not, such as a string where another character sits where the period should be.