Download Cheat sheet PDF 12 pages · syntax, editors, patterns, Unicode, performance, debugging
Pattern

Regex for Quoted string

Match either single- or double-quoted strings.

The Quoted string regex is (["\'])(?:(?=(\\?))\2.)*?\1 — copy it below, or open it in the explainer for a token-by-token breakdown.

The pattern

(["\'])(?:(?=(\\?))\2.)*?\1
Try in explainer → Download cheat sheet ↓

What it matches

  • "hello"
  • 'world'
  • "escape \" inside"

What it doesn't match

  • no quotes
  • "unclosed

Notes & gotchas

Matches paired quotes (same kind on both sides) and handles escaped quotes inside. For simple needs, "[^"]*" is faster but doesn't handle escapes.

Code in your language

Use the explainer's Code tab to generate ready-to-paste snippets in JavaScript, Python, Java, .NET, Go, Ruby, and PHP for this pattern.

Open in explainer →

Token-by-token breakdown

Every part of the pattern, left to right:

TokenMeaning
(start of a capturing group
["\']any of: “"”, “'”
)end of group
(?:start of a non-capturing group
(?=start of a positive lookahead — followed by
(start of a capturing group
\\?optional (zero or one): literal “\”
)end of group
)end of group
\2backreference to group 2
.any character (except newline)
)*?end of group, zero or more (lazy — as few as possible)
\1backreference to group 1

About this pattern

Number patterns appear constantly in form validation, log parsing, and data extraction. Regex is great for matching the shape but limited for semantic checks (range validation, mathematical constraints).

Quick usage in different languages

This exact pattern — with the correct escaping and idioms for each language:

  • JavaScript: /(["\'])(?:(?=(\\?))\2.)*?\1/.test(value)
  • Python: re.match(r"""(["\'])(?:(?=(\\?))\2.)*?\1""", value)
  • Java: Pattern.compile("([\"\\'])(?:(?=(\\\\?))\\2.)*?\\1").matcher(value).matches()
  • C# / .NET: Regex.IsMatch(value, @"([""\'])(?:(?=(\\?))\2.)*?\1")
  • Go: regexp.MustCompile(`(["\'])(?:(?=(\\?))\2.)*?\1`).MatchString(value)
  • Ruby: /(["\'])(?:(?=(\\?))\2.)*?\1/.match?(value)
  • PHP: preg_match('~(["\'])(?:(?=(\\?))\2.)*?\1~', $value)

The explainer’s Code tab regenerates these for any pattern you paste, and the downloadable cheat sheet bundles the breakdown, all seven snippets, and the pitfalls below onto one printable page.

Common pitfalls

  • Not anchored. Without ^ and $ this can match a substring anywhere in the input — add anchors if you need the whole value to conform.
  • Escape it correctly per language. In Java and JavaScript strings each backslash must be doubled (\\d); in Python, Go, and C# use raw/verbatim strings so the backslashes survive.
  • Validate beyond format. Matching the format doesn't guarantee the value is real. Confirm the quoted string against a source of truth (database, API, or checksum) where it matters.

Related patterns

More patterns in the Numbers & text category:

See also

Browse all 300 patterns in the library, or open this regex in the interactive explainer for a token-by-token breakdown, live testing, and code in seven languages.


Want more patterns? Browse the full library →