Regex to Match JSON String Values

Match properly escaped JSON string values including those with backslash escape sequences like \n, \t, and \uXXXX unicode escapes. Free regex tester.

Regular Expression

/"(?:[^"\\]|\\.)*"/g

Token Breakdown

TokenDescription
"Matches the literal character '"'
(?:Start of non-capturing group
[^"\\]Negated character class — matches any character NOT in "\\
|Alternation — matches the expression before OR after the pipe
\\Escaped character '\'
.Matches any character except newline (unless dotAll flag is set)
)End of group
*Matches the preceding element zero or more times (greedy)
"Matches the literal character '"'

Detailed Explanation

This regex matches JSON string values that are properly delimited and escaped. Here is the token-by-token breakdown:

" — Matches the opening double quote that begins every JSON string value.

(?: — Opens a non-capturing group for the content alternatives that can appear inside the string.

[^"\] — First alternative: matches any single character that is not a double quote or backslash. These are the plain, unescaped characters in the string. The double quote is excluded because it would end the string, and the backslash is excluded because it begins an escape sequence.

| — Alternation operator.

\. — Second alternative: matches an escape sequence consisting of a backslash followed by any character. This handles all JSON escape sequences including \n (newline), \t (tab), " (escaped quote), \\ (escaped backslash), / (escaped slash), and \uXXXX (Unicode escapes). The backslash itself is escaped in the regex.

)* — The non-capturing group repeats zero or more times, allowing strings of any length including empty strings.

" — Matches the closing double quote that ends the JSON string.

The g flag enables global matching to find all JSON strings in the input. This pattern is essential for JSON parsing, syntax highlighting, string extraction, and data processing. It correctly handles strings with embedded escape sequences, empty strings, and strings containing special characters. It can be used to identify all string values in a JSON document for further processing or validation.

Example Test Strings

InputExpected
"hello world"Match
"escaped \"quotes\""Match
"line\nbreak"Match
'single quotes'No Match
"unicode \u00A9"Match

Try It — Interactive Tester

//g
gimsuy

Match Highlighting(4 matches)

"hello world" "escaped \"quotes\"" "line\nbreak" 'single quotes' "unicode \u00A9"

Matches & Capture Groups

#1"hello world"index 0
#2"escaped \"quotes\""index 14
#3"line\nbreak"index 35
#4"unicode \u00A9"index 65
Pattern: 17 charsFlags: gMatches: 4

Ctrl+Shift+C to copy regex

Customize this pattern →