Webflags: gi
CSS Class from `class=""` Attribute
Extract the value of a `class="..."` attribute from raw HTML, handling double or single quotes.
Try it in RegexPro →Available in
Pattern
regexengine-agnostic
class\s*=\s*["']([^"']+)["'] (flags: gi)Raw source: class\s*=\s*["']([^"']+)["']
How it works
class\s*=\s* matches the attribute name with optional whitespace around the `=`. ["'] matches either quote style. ([^"']+) captures everything until the matching quote (the class list itself, possibly multiple classes). The closing ["'] matches the same quote style as opened — note this regex doesn't enforce that they MATCH, but in valid HTML they will.
Examples
Input
<div class="btn primary">Matches
class="btn primary"
Input
<span class='card'>Matches
class='card'
Input
<div id="foo">No match
—Common use cases
- •HTML scraping and class auditing
- •Migrating CSS classes during refactors
- •Bundler / lint tooling that reports unused classes
- •Generating component manifests from JSX/HTML