Smart quotes, non-breaking spaces and zero-width characters are the usual culprits. Here is how to find and remove them.
The short version
Start here if you only have one minute. The full guide below explains each point.
Smart quotes are the usual cause
Word processors substitute them automatically.
Some are invisible
Zero-width characters break comparisons silently.
Show invisibles
One editor setting catches most of these.
01
Smart quotes are the most common cause
Word processors and many websites replace straight quotes with curly typographic ones. They look better and are not valid string delimiters in any programming language.
Pasting code through a document is how they get in. The error message rarely points at the quote, which is what makes this waste so much time.
- " straight quote, U+0022, valid in code
- “ and ” curly quotes, U+201C and U+201D, not valid
- ' straight apostrophe, U+0027, valid
- ’ right single quote, U+2019, not valid
02
Non-breaking spaces look like spaces
A non-breaking space at U+00A0 renders identically to an ordinary space and is a completely different character to a parser.
They come from copying out of rendered web pages and out of documents. In YAML and Python, where whitespace is structural, they break the file outright.
03
Zero-width characters are invisible
Zero-width spaces and joiners take no space and draw nothing. They can sit inside an identifier and make two apparently identical names different.
This is the hardest class to diagnose, because the file looks correct in every editor that does not explicitly reveal them.
✦If two identical-looking strings compare as unequal, an invisible character is the first thing to check.
04
Dashes and minus signs
An en dash or a minus sign pasted where a hyphen belongs breaks command-line flags and CSS values. Autocorrect creates these without being asked.
The hyphen-minus at U+002D is the only one valid in code. Every visually similar character is a different one.
05
Finding them
Turn on invisible character display in your editor. Most support it and few people enable it.
A search for the specific code points works too, and a linter configured to flag non-ASCII characters in source files catches them before they ship.
- Enable show invisibles in the editor
- Search for U+00A0, U+200B and the curly quotes
- Add a linter rule for unexpected non-ASCII characters
06
Preventing them
Never route code through a word processor. Paste as plain text, and turn off smart quote substitution in any tool you write code in.
In content management systems, the editor may apply typographic substitution to anything in a code field. Check the stored value rather than the preview.
The characters that break code
Each pair looks near-identical on screen. Only the right-hand column is valid in source code.
| Looks like | Actually is | Valid substitute |
|---|---|---|
| “hello” | U+201C and U+201D | " U+0022 |
| it’s | U+2019 | ' U+0027 |
| a space | U+00A0 non-breaking space | space U+0020 |
| nothing | U+200B zero-width space | delete it |
| - hyphen | U+2013 en dash | - U+002D |
The fourth row has no visual appearance at all, which is why it survives every proofread and only surfaces as a failing comparison.
07
Copy the symbols discussed in this guide
This guide explains the topic. Use invisible characters for the main copyable set, compare it with quotation mark symbols, and browse punctuation symbols for another useful angle. Each collection includes searchable Unicode names and HTML references.
08
Questions, answered
Why does my code break after pasting from a document?+
The document almost certainly replaced straight quotes with curly ones, or inserted non-breaking spaces. Neither is valid in source code.
How do I find invisible characters in a file?+
Enable invisible character display in your editor, or search for the specific code points such as U+00A0 and U+200B.
Is a non-breaking space the same as a space?+
No. It looks identical and is a different character, U+00A0, which breaks any parser expecting an ordinary space.
09
Sources and review notes
Recommendations were checked against published platform, Unicode and accessibility guidance. This is a standards review, not a claim of testing every screen reader, app or device combination.
