BytePane

Data Attribute Regex Pattern

Matches HTML5 data attributes (data-*) and captures both the attribute name and value.

🌐
Web & HTML
Beginner
Difficulty
HTML
Language
gi
Flags
// Regular Expression
/data-([a-z-]+)=["']([^"']*)["']/gi

Live Regex Tester

Pattern Breakdown

data-([a-z-]+)=["']([^"']*)["']
Character class [ ]
Group ( )
Quantifier { }
Anchor ^ $
Repetition * + ?
Escape \
Alternation |
Any char .

Code Examples

JavaScript

const regex = /data-([a-z-]+)=["']([^"']*)["']/gi;
const test = "data-user-id="123"";
console.log(regex.test(test)); // true

// Extract matches
const matches = test.match(regex);
console.log(matches);

Python

import re

pattern = r'data-([a-z-]+)=["']([^"']*)["']'
test = "data-user-id="123""
match = re.findall(pattern, test, re.IGNORECASE)
print(match)  # Found!

Go

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile(`data-([a-z-]+)=["']([^"']*)["']`)
    fmt.Println(re.MatchString("data-user-id="123"")) // true
}

Common Use Cases

DOM manipulationJavaScript frameworkstesting selectors

Match Examples

InputResult
data-user-id="123"Match
class="test"No Match

About the Data Attribute Regex

Matches HTML5 data attributes (data-*) and captures both the attribute name and value.

Regular expressions (regex) are powerful pattern matching tools used across virtually all programming languages. The data attribute pattern is classified as beginner difficulty in the web & html category. This pattern is specifically designed for HTML.

When using this regex, always consider edge cases and test thoroughly with real-world data. Use the interactive tester above to validate the pattern against your specific inputs before deploying to production.

Need More Regex Patterns?

Browse our complete library of 100+ regex patterns with interactive testers.

Frequently Asked Questions

What is the Data Attribute regex pattern?

Matches HTML5 data attributes (data-*) and captures both the attribute name and value.

How do I use the Data Attribute regex?

Use the pattern /data-([a-z-]+)=["']([^"']*)["']/gi in your code. In JavaScript: new RegExp('data-([a-z-]+)=["\']([^"\']*)["\']', 'gi'). Test it above with your own input.

What does this Data Attribute regex match?

This pattern matches: "data-user-id="123"". It does NOT match: "class="test"". DOM manipulation, JavaScript frameworks, testing selectors.

Is the Data Attribute regex beginner-friendly?

This pattern is rated Beginner. It uses basic regex syntax and is easy to understand.

What languages support the Data Attribute regex?

This pattern works in HTML. Syntax may vary slightly between regex engines.

Can I modify the Data Attribute regex for my use case?

Yes! Use the interactive tester above to modify the pattern and test with your own data. Common modifications include making it case-insensitive (add 'i' flag), matching globally (add 'g' flag), or adjusting character classes.

Related Tools