Naar de inhoud
OneKitly

Regex Basics: A Beginner's Guide

Gepubliceerd op 10-10-2025 · 3 min leestijd · Ontwikkelaarstools

Daniel Okonkwo

Daniel Okonkwo — Front-enddeveloper en techredacteur bij OneKitly

Webprestaties · Bestandsformaten

Getoetst aan 2 bronnen

Profiel bekijken →
Kort gezegd

A regular expression (regex) is a pattern for matching text. The essentials: literal characters match themselves, . matches any character, \d matches a digit, \w a word character, \s whitespace; * means zero or more, + one or more, ? optional; [abc] matches any listed character, ^ and $ anchor to the start and end. So \d{3}-\d{4} matches a phone number like 555-1234.

Advertentie

A regular expression is a pattern for matching text. Here are the building blocks — character classes, quantifiers and anchors — with a worked example.

What regex is for

A regular expression describes a pattern rather than an exact string, which lets you search, validate, extract and replace text far more flexibly. Instead of hunting for one fixed phone number, you describe the shape of any phone number and match them all. They power find-and-replace in editors, form validation, and pulling data out of logs and text.

Advertentie

The building blocks

Most of a pattern is literal text that matches itself. Where text varies, you use character classes: \d matches any digit, \w a word character (letters, digits, underscore), \s any whitespace, and the dot . matches almost any single character. A set in square brackets like [aeiou] matches any one character you list, and [^0-9] matches anything except those.

Quantifiers and anchors

Quantifiers say how many times the previous item may repeat: * is zero or more, + is one or more, ? is optional (zero or one), and {3} means exactly three, {2,4} between two and four. Anchors pin the match in place: ^ marks the start of the text and $ the end, so ^abc$ matches only the exact string 'abc'. Parentheses ( ) group parts together.

A worked example

To match a phone number like 555-1234, describe its shape: three digits, a hyphen, four digits. That is \d{3}-\d{4}. To also allow an optional area code in brackets, you might extend it step by step, testing as you go. Note that quantifiers are greedy by default — they grab as much as they can — which is worth remembering once patterns get longer.

Regex-testerTest een reguliere expressie en zie wat die doet: overeenkomsten opgelicht in de tekst, elke capturegroep op een rij, een vervangpaneel, en het patroon stuk voor stuk aan je teruggelezen. Vlaggen zijn schakelaars die zeggen wat ze veranderen, en een patroon dat de pagina kan laten vastlopen, wordt gemeld voordat je het uitvoert.Probeer de tool →
Advertentie

Veelgestelde vragen

What does \d mean in regex?
It matches any single digit, 0 through 9. So \d{3} matches exactly three digits in a row.
What's the difference between * and +?
* matches zero or more of the preceding item, while + matches one or more — so + requires at least one occurrence.
Are regex the same in every language?
The core syntax is shared, but details and flavours differ between JavaScript, Python, PCRE and others — check your language's docs for edge cases.

Artikelen die je misschien interesseren

Alle gidsen →
VergelijkingJSON vs XML: What's the Difference?JSON and XML both store structured data as text, but they trade off differently. Here's how each looks, where each wins, and how to choose.GidsZoeken en vervangen: de regexfuncties die bijten, één voor één gedemonstreerdGulzig tegen lui op dezelfde tekenreeks, de punt die regeleindes overslaat, $& en $$ in de vervanging, een hergebruikte /g-regex die stil een regel overslaat, en waarom /i niets weet over de Turkse i. Elke storing in Node uitgevoerd, met een routine die eerst telt en dan vervangt en ze betrapt.UitlegExtracting Every Email Address or URL from a Block of TextA URL at the end of a sentence keeps the full stop; an email address at the end of the same sentence does not. An accented first name in an address comes back truncated. Every case here was run through the tools and the exact output is reported.UitlegcamelCase vs. snake_case: een gids voor naamgevingsconventies in codecamelCase, snake_case, PascalCase en kebab-case uitgelegd: hoe ze eruitzien, waar ze de conventie zijn en hoe je er consequent één kiest.UitlegEscaping a String for JSON: Three Characters Are Mandatory, and One Is a TrapRFC 8259 requires exactly three things to be escaped inside a JSON string. Everything else is optional. The one that actually breaks pipelines is a lone surrogate — legal in JSON text, impossible in UTF-8, and silently replaced the moment your data is written out.UitlegXML to JSON: Attributes, Repetition, and the Single-Element Array TrapTwo documents that differ only in how many children exist produce two different JSON shapes, and no converter can tell them apart without a schema. Plus what this one really does with attributes, mixed content and whitespace — and the one thing it still cannot record.

Vergelijkbare tools

Bronnen

Een fout in dit artikel gezien?