🕵️ SicherheitslückenWhat continuous operational resilience looks like under DORA(09.09.2026 um 17:53 Uhr)
🔧 AI Nachrichten OpenAI seeks tougher AI rules. CIOs may feel the ripple effects(10.09.2026 um 12:11 Uhr)
🔧 AI Nachrichten Mistral valued at €21bn after €3bn Series D funding round(08.09.2026 um 10:19 Uhr)
🪟 Windows TippsWindows XP's Cursor Indicator Is Getting a Windows 11 Refresh(25.08.2026 um 13:00 Uhr)
🕵️ SicherheitslückenWhat continuous operational resilience looks like under DORA(09.09.2026 um 17:53 Uhr)
🔧 AI Nachrichten OpenAI seeks tougher AI rules. CIOs may feel the ripple effects(10.09.2026 um 12:11 Uhr)
🔧 AI Nachrichten Mistral valued at €21bn after €3bn Series D funding round(08.09.2026 um 10:19 Uhr)
🪟 Windows TippsWindows XP's Cursor Indicator Is Getting a Windows 11 Refresh(25.08.2026 um 13:00 Uhr)

🔧 Programmierung 🕛 vor 2 Monaten 4 Min Lesezeit
0

The Complete Guide to Writing Better Regular Expressions

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Originally published on provides real-time highlighting, flag toggles, and named group detection. Always test your pattern against multiple input strings before deploying it.






Understanding Regex Flags



Flags change how a regex pattern is interpreted. Here are the six most important flags in JavaScript:











































Flag Name Effect
g Global Find all matches, not just the first one
i Case-insensitive Match both uppercase and lowercase
m Multiline
^ and $ match start/end of each line
s Dot All Make . match newline characters too
u Unicode Enable Unicode property escapes like \p{L}
y Sticky Match only from lastIndex position





Practical Patterns for Everyday Use






Email Validation



A robust email regex is surprisingly complex. The official RFC 5322 regex is hundreds of characters long. For practical purposes, this pattern covers 99.9% of real email addresses:




CODE
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/









URL Extraction






CODE
/https?:\/\/[\w\-._~:\/?#\[\]@!$&'()*+,;=]+/g









Password Strength (at least 8 chars, 1 upper, 1 lower, 1 digit)






CODE
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/






This uses lookaheads ((?=...)) to check each condition without consuming characters.






Extract All Hex Colors from CSS






CODE
/#([a-fA-F0-9]{3}|[a-fA-F0-9]{6})\b/g









Named Capture Groups



JavaScript (ES2018+) supports named groups, which make your regex much more readable:




CODE
const logPattern = /(?<ip>\d+\.\d+\.\d+\.\d+) - - \[(?<date>[^\]]+)\]/;
const match = logPattern.exec('192.168.1.1 - - [19/Jun/2026:12:00:00]');
console.log(match.groups.ip); // "192.168.1.1"
console.log(match.groups.date); // "19/Jun/2026:12:00:00"






Named groups make complex patterns self-documenting and eliminate fragile index-based group references like match[1].






Common Regex Traps






1. Catastrophic Backtracking



Nested quantifiers like (a+)+b can cause exponential backtracking. On a long string of "a"s without a "b" at the end, the engine tries every possible split before giving up. This can freeze your application.



Fix: Use atomic groups (if supported) or rewrite to avoid nested quantifiers. Use possessive quantifiers like a++ where available.






2. Greedy vs Lazy Matching



By default, quantifiers are greedy — they match as much as possible. To match the minimum, add ? after the quantifier:




CODE
// Greedy: matches "<div>...</div><span>..." as one match
/<.*>/

// Lazy: matches each tag individually
/<.*?>/









3. Forgetting the Global Flag



Without the g flag, regex.exec() and String.match() return only the first match. Always add g when you need all occurrences.






Testing Your Regex



Even experienced developers write buggy regex on the first try. Always test your patterns against:





  • Valid input — does it match what you want?


  • Invalid input — does it correctly reject bad data?


  • Edge cases — empty strings, very long strings, strings with special characters


  • Performance — test with a large input to catch backtracking issues



Use the for more free online developer tools. All tools run locally in your browser — your data never leaves your device.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Sam Altman calls GPT-6 Astra rollout ‘messy’ as enterprise users wait for access
1 Quelle
Swiss government explores replacing Microsoft 365 with open-source software
1 Quelle
What continuous operational resilience looks like under DORA
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten The Complete Guide to Writing Better Regular Expressions

Thematisch verwandte Begriffe: Complete, Guide, Writing, Better · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...