Ihr Muster wird als regulärer Ausdruck von JavaScript übersetzt und gegen Ihren Testtext ausgeführt, und jeder Treffer wird mit einer Zählung aufgelistet. Die Flags gibt man getrennt an: g findet jeden Treffer statt beim ersten aufzuhören, i ignoriert die Groß- und Kleinschreibung, m lässt die Anker am Anfang und Ende jeder Zeile greifen, und s lässt einen Punkt auch einen Zeilenumbruch treffen.
Die Regex-Maschine hier ist die des Browsers, also dieselbe, die auch Ihr JavaScript nutzt. Das ist wissenswert, weil die Syntax nicht universell ist: Muster für PHP, Python oder einen Texteditor nutzen mitunter Funktionen, die JavaScript nicht hat, und besonders Lookbehind hat eine andere Unterstützungsgeschichte. Lässt sich das Muster nicht übersetzen, erscheint die Meldung der Maschine selbst statt eines allgemeinen Fehlers.
Ein Muster bauen, das trägt
-
Ohne das g-Flag bekommen Sie einen Treffer:
Die Zählung endet beim ersten Vorkommen — der übliche Grund, warum ein funktionierendes Muster weit weniger zu finden scheint, als es sollte.
-
Testen Sie gegen Text, der nicht passen soll:
Ein Muster, das trifft, was Sie wollen, ist nur die halbe Miete. Die Fehler, die in den Produktivbetrieb gelangen, sind Muster, die mehr treffen als beabsichtigt.
-
Verschachtelte Quantoren können die Seite blockieren:
Ein Muster mit einer Wiederholung innerhalb einer anderen kann bei fast passender Eingabe exponentiell lange brauchen. Wirkt ein Test eingefroren, liegt es meist daran.
-
Maskieren Sie, was wörtlich gemeint ist:
Ein Punkt trifft jedes Zeichen, solange er nicht maskiert ist — deshalb treffen Muster für einen Dateinamen oder eine Adresse stillschweigend weit mehr als erwartet.
Compiled by the same engine your JavaScript will use
A pattern entered into the [msm_tool slug="regex-tester"] is compiled as an actual JavaScript regular expression and run for real, using the browser's own engine — the same one any JavaScript on the page would use. That matters because regex syntax is not universal: a pattern written for PHP, Python or a text editor can rely on features JavaScript does not have, and lookbehind in particular has a genuinely uneven support history across engines.
Without the g flag, you get exactly one match
Leaving the global flag off stops the search at the very first occurrence, which is the ordinary reason a pattern that clearly looks correct appears to find far less than it should. Adding it is usually the entire fix for a match count that seems inexplicably low.
Nested quantifiers can hang the page
A pattern with one repetition nested inside another can take exponential time against input that almost matches but not quite, which is exactly the shape of input most likely to appear in practice. If a test seems to freeze rather than simply run slowly, this is almost always why — and the same pattern would cause the identical problem running in production, not just here.
A dot matches everything until it is told not to
An unescaped dot matches any character at all, which is why a pattern meant to match a filename or an email address quietly matches a great deal more than intended unless the dot is escaped where a literal period is actually meant. This is one of the most common gaps between what a pattern was meant to do and what it actually does.
Test against what should not match, not only what should
A pattern that correctly matches the intended input is only half the job done. The mistakes that make it into production are almost always patterns that match more than intended, which only shows up by deliberately testing against text the pattern is supposed to reject.
What it will not show you
Whole matches are listed along with a total count, but individual capture groups are not broken out separately here. Inspecting a pattern group by group means running it in actual code, where each group can be read directly rather than inferred from the whole match.