How to use the JS quote normalizer
- Paste your code into the JavaScript box.
- Pick Quotes: Single ’ (the default) or Double “.
- Keep Keep the original quote when switching would need extra escapes ticked to avoid strings full of backslashes.
- Copy the Normalised code and check Strings re-quoted.
What it does and when to use it
Mixed quote styles happen when code is copied from several sources or written by several people. Linters such as ESLint’s quotes rule then report dozens of warnings. This tool fixes them in one go for a pasted file or snippet, without the risk of a find-and-replace that also changes quotes inside comments, regexes or other strings.
In a project, a formatter keeps quotes consistent on every save; the JavaScript formatter has a quote option too. To turn backtick templates into plain strings first, use the template literal converter. To escape a single piece of text for a string by hand, the string escaper handles that.
How it works
The lexer splits the code into tokens and only looks at single- and double-quoted string tokens. For each one in the other style, it:
- Drops the backslash from escaped old quotes, so
\'becomes'when moving to double quotes. - Adds a backslash before any new quote character in the text, so
"say 'hi'"becomes'say \'hi\''. - Keeps every other escape, such as
\noré, as written.
With the keep option on, a string that contains the new quote character but not the old one is left exactly as it is.
Worked examples
The default sample, to single quotes.
const a = "hello";
const b = 'it\'s';
const c = "say \"hi\"";
// "comments" are untouched
const re = /"x"/;
becomes:
const a = 'hello';
const b = 'it\'s';
const c = 'say "hi"';
// "comments" are untouched
const re = /"x"/;
Two strings were re-quoted. Line c lost its backslashes, because double quotes no longer need escaping inside single quotes. The comment and the regex did not change.
Keeping an apostrophe readable. const c = "it's"; with the keep box ticked stays the same, and Strings re-quoted is 0.
Forcing the switch. The same line with the box unticked becomes const c = 'it\'s';.
Limits and tips
- Strings only. Object keys written without quotes and template literals are not changed.
- Run a linter afterwards if your style guide has extra rules, such as allowing the other quote to avoid escapes.
- Directives such as
"use strict"are strings too and will be re-quoted; both forms work the same.
Frequently asked questions
Why was one of my strings not changed?
Are template literals converted?
Does it change JSON?
Will it change quotes inside JSX attributes?
How do we know nothing is uploaded? Test it yourself on the privacy proof page.