{"id":28418738,"url":"https://github.com/eslamalawy/regex","last_synced_at":"2025-10-27T18:07:02.273Z","repository":{"id":295375809,"uuid":"989933125","full_name":"eslamalawy/regex","owner":"eslamalawy","description":"Regular Expression","archived":false,"fork":false,"pushed_at":"2025-05-25T07:07:01.000Z","size":35,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-06-04T18:33:53.774Z","etag":null,"topics":["regex","regular-expressions","summary","tutorial"],"latest_commit_sha":null,"homepage":"https://regex-eslam.netlify.app/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eslamalawy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-05-25T06:25:48.000Z","updated_at":"2025-05-25T07:13:43.000Z","dependencies_parsed_at":"2025-05-25T08:20:09.004Z","dependency_job_id":null,"html_url":"https://github.com/eslamalawy/regex","commit_stats":null,"previous_names":["eslamalawy/regex"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eslamalawy/regex","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eslamalawy%2Fregex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eslamalawy%2Fregex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eslamalawy%2Fregex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eslamalawy%2Fregex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eslamalawy","download_url":"https://codeload.github.com/eslamalawy/regex/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eslamalawy%2Fregex/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262375594,"owners_count":23301314,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["regex","regular-expressions","summary","tutorial"],"created_at":"2025-06-04T12:20:54.833Z","updated_at":"2025-10-27T18:06:57.248Z","avatar_url":"https://github.com/eslamalawy.png","language":"JavaScript","readme":"# Regular Expressions Summary\n\nAfter studying a complete regex course, I've created this guide along with a simple website featuring practical examples. \n\nCheck it out: [![Netlify](https://img.shields.io/badge/Netlify-Deployed-blue?logo=netlify)](https://regex-eslam.netlify.app/)\n\n## 📚 Table of Contents\n\n1. [Explicit Characters and Quantifiers](#1-explicit-characters-and-quantifiers)\n2. [Collections, Character Ranges, and Negation](#2-collections-character-ranges-and-negation)\n3. [Whitespace Characters and String Boundaries](#3-whitespace-characters-and-string-boundaries)\n4. [Character Classes](#4-character-classes)\n5. [Flags](#5-flags)\n6. [Greedy vs. Lazy Quantifiers](#6-greedy-vs-lazy-quantifiers)\n7. [Multi-Character Strings Quantifiers and Options](#7-multi-character-strings-quantifiers-and-options)\n8. [Capture Groups](#8-capture-groups)\n9. [Substitution (Replace)](#9-substitution-replace)\n10. [Lookarounds](#10-lookarounds)\n\n---\n\n## 1. Explicit Characters and Quantifiers\n\n### Basic Quantifiers\n\n| Quantifier | Description | Behavior |\n|------------|-------------|----------|\n| `?` | Zero or one times | Matches the previous token between zero and one times (greedy) |\n| `*` | Zero or more times | Matches the previous token between zero and unlimited times (greedy) |\n| `+` | One or more times | Matches the previous token between one and unlimited times (greedy) |\n\n**Note:** If no quantifier is specified, it matches exactly once.\n\n### Special Characters\n\n- **Period (`.`)** - Matches any character except line terminators (`\\n`)\n\n### Characters That Need Escaping\n\nThese characters have special meaning in regex and must be escaped with backslash (`\\`) to match them literally:\n\n```\n+ ? . { } [ ] ( ) ^ $\n```\n\n**Examples:**\n```regex\n\\+ \\? \\. \\{ \\} \\[ \\] \\( \\) \\^ \\$\n```\n\n### Curly Brace Quantifiers `{}`\n\n| Syntax | Description |\n|--------|-------------|\n| `{x,y}` | Between x and y times (inclusive) |\n| `{x}` | Exactly x times |\n| `{x,}` | x or more times |\n\n**Examples:**\n- `{1,3}` - Between one and three times\n- `{3}` - Exactly three times  \n- `{4,}` - Four or more times\n\n---\n\n## 2. Collections, Character Ranges, and Negation\n\n### Collections\n\nSquare brackets `[]` match **one** of any specified characters:\n\n```regex\n[0123456789ABCDEF]  # Matches any single hexadecimal digit\n[0-9A-F]+           # One or more hexadecimal digits (using ranges)\n```\n\n**Hexadecimal Reference:**\n- Base 16 numbering system\n- Digits: 0-9, A(10), B(11), C(12), D(13), E(14), F(15)\n\n### Character Ranges\n\nUse hyphen (`-`) inside collections to create ranges:\n\n```regex\n[0-9]    # Numbers 0 through 9\n[A-Z]    # Uppercase letters A through Z\n[a-z]    # Lowercase letters a through z\n[0-9A-F] # Hexadecimal digits\n```\n\n### Negation\n\nUse caret (`^`) as the **first character** inside collections for negation:\n\n```regex\n[^a-z4]  # Anything BUT lowercase letters a-z or the number 4\n[^\\.?!]  # Anything BUT period, question mark, or exclamation point\n```\n\n### Practical Example\n\n```regex\n/[A-Z][^\\.?!]+[\\.?!]/\n```\n\n**Breakdown:**\n- `[A-Z]` - Exactly one capital letter\n- `[^\\.?!]+` - One or more characters that are NOT `.`, `?`, or `!`\n- `[\\.?!]` - Ends with one of: `.`, `?`, or `!`\n\n**Note:** Period (`.`) needs escaping inside collections, but `?` and `!` don't.\n\n---\n\n## 3. Whitespace Characters and String Boundaries\n\n### Whitespace Characters\n\nBackslash (`\\`) serves dual purposes:\n1. **Escape special characters:** `\\.` (literal period)\n2. **Create special tokens:** `\\t` (tab character)\n\n| Token | Description |\n|-------|-------------|\n| `\\t` | Tab character |\n| `\\n` | New line |\n| `\\r` | Carriage return |\n| `\\f` | Form feed |\n| `\\v` | Vertical tab |\n| `\\r\\n` | Windows-style new line |\n\n### Space Character\n\nNo backslash needed for regular spaces:\n\n```regex\n/ /      # Single space\n/ +/     # One or more spaces\n/ +.*/   # One or more spaces followed by any characters\n```\n\n### String Boundaries\n\n| Anchor | Description |\n|--------|-------------|\n| `^` | Start of string |\n| `$` | End of string |\n\n**Important:** These don't match characters, they indicate position.\n\n```regex\n/^ha$/     # Matches only \"ha\" (entire string)\n/^ +.*/    # Must start with one or more spaces\n```\n\n### Caret (`^`) Double Duty\n\n1. **String boundary:** `^` (start of string)\n2. **Negation:** `[^246]` (not 2, 4, or 6)\n\n### Complete Example\n\n```regex\n/^[A-Z][^\\.?!]+[\\.?!]$/\n```\n\n**Breakdown:**\n- `^[A-Z]` - Must start with a capital letter\n- `[^\\.?!]+` - One or more characters that are not `.`, `?`, or `!`\n- `[\\.?!]$` - Must end with `.`, `?`, or `!`\n\n---\n\n## 4. Character Classes\n\n### Basic Character Classes\n\n| Class | Description | Equivalent |\n|-------|-------------|------------|\n| `.` | Any character except newline | - |\n| `\\s` | Any whitespace character | `[\\r\\n\\t\\v\\f ]` |\n| `\\S` | Any non-whitespace character | `[^\\r\\n\\t\\v\\f ]` |\n| `\\d` | Any digit | `[0-9]` |\n| `\\D` | Any non-digit | `[^0-9]` |\n| `\\w` | Any word character | `[0-9A-Za-z_]` |\n| `\\W` | Any non-word character | `[^0-9A-Za-z_]` |\n\n### Word Boundaries\n\n| Boundary | Description |\n|----------|-------------|\n| `\\b` | Word boundary |\n| `\\B` | Not a word boundary |\n\n### Word Boundary Examples\n\n```regex\n\\bcat\\b   # Matches 'cat' in \"a black cat\" but not in \"catatonic\"\n✅ it like this:        \\W(=space)      \\b      \\W(=line terminator)\n\n\\bcat     # Matches 'cat' in \"catfish\"\n✅ it like this:        \\W(=start of the line)      \\b      \\w(=f)\n✅ or default  :        \\W      \\b      \\W\n\ncat\\b     # Matches 'cat' in \"tomcat\"\n✅ it like this:        \\w(=m)     \\b      \\W(=line terminator)\n✅ or default  :        \\W      \\b      \\W\n```\n\n**Word boundary conditions:**\n- `\\b` occurs between\n  - `\\W` and `\\W` characters  (default)\n  - `\\W` and `\\w` characters\n  - `\\w` and `\\W` characters\n\n- `\\B` occurs when both sides are a word character\n  - `\\w` and `\\w` characters\n\n### Practical Examples\n\n```regex\n/^\\s+/     # Find whitespace at beginning of string\n/^\\S+/     # Must start with non-whitespace characters\n/^\\d+/     # Must start with one or more digits\n```\n\n---\n\n## 5. Flags\n\nFlags modify how the regex engine interprets the pattern:\n\n| Flag | Name | Description |\n|------|------|-------------|\n| `g` | Global | Match as many times as possible (not just once) |\n| `m` | Multi-line | `^` and `$` match start/end of each line, not just string |\n| `i` | Case-insensitive | Match both upper and lowercase letters |\n| `s` | Single-line | `.` matches newline characters (treats string as single line) |\n\n### Usage Examples\n\n```regex\n/pattern/g    # Global matching\n/pattern/i    # Case-insensitive\n/pattern/gim  # Multiple flags combined\n```\n\n---\n\n## 6. Greedy vs. Lazy Quantifiers\n\n### Understanding Greedy vs. Lazy\n\n- **Greedy:** Take everything you can and still match\n- **Lazy:** Take as little as you can to still match\n\n### Default Behavior\n\n```regex\n/gre*/  # Greedy by default - matches as much as possible\n```\n\n### Making Quantifiers Lazy\n\nAdd `?` after the quantifier:\n\n| Greedy | Lazy | Description |\n|--------|------|-------------|\n| `*` | `*?` | Zero or more (lazy) |\n| `+` | `+?` | One or more (lazy) |\n| `?` | `??` | Zero or one (lazy) |\n| `{n,m}` | `{n,m}?` | Between n and m (lazy) |\n\n### Practical Applications\n\n**Sentence matching comparison:**\n\n```regex\n# Using character negation (previous approach)\n/^[A-Z][^\\.?!]+[\\.?!]$/\n\n# Using lazy quantifiers  \n/^[A-Z].+?[\\.?!]$/\n```\n\n**Lazy quantifier examples:**\n- `.+?` - One or more characters (lazy)\n- `.*?` - Zero or more characters (lazy)\n\nBoth will match minimal characters before the final punctuation.\n\n---\n\n## 7. Multi-Character Strings Quantifiers and Options\n\n### Multi-Character Options\n\nUse pipe (`|`) for \"OR\" logic with multi-character strings:\n\n```regex\n/kittens|foals|ducklings/  # Matches any of these three words\n```\n\n### Groups for Multi-Character Tokens\n\n**Syntax:** Parentheses `()`\n\n```regex\n/I love (kittens|foals|ducklings)/  # \"I love \" + any of the three options\n```\n\n### Quantifiers with Groups\n\n```regex\n/(kittens)+/  # One or more occurrences of the string \"kittens\"\n```\n\n### Three Ways to Handle \"kittens\"\n\n1. **No container:** `/kittens+/`\n   - Quantifier applies only to last character ('s')\n   - Matches: \"kitten\", \"kittens\", \"kittenss\", etc.\n\n2. **Square brackets:** `/[kittens]+/`\n   - Quantifier applies to character collection\n   - Matches: one or more of any letters k, i, t, e, n, s\n\n3. **Parentheses:** `/(kittens)+/`\n   - Quantifier applies to entire group\n   - Matches: \"kittens\", \"kittenskittens\", etc.\n\n### Practical Example: Digital Clock (24-hour format)\n\n**Requirements:**\n- Hours: 0-23\n- Minutes: 00-59\n\n```regex\n/(1?\\d|2[0-3]):[0-5]\\d/\n```\n\n**Breakdown:**\n- `(1?\\d|2[0-3])` - Hours group:\n  - `1?\\d` - Optional 1 + any digit (0-19)\n  - `|` - OR\n  - `2[0-3]` - 2 + digits 0-3 (20-23)\n- `:` - Literal colon\n- `[0-5]\\d` - Minutes: first digit 0-5, second digit 0-9 (00-59)\n\n### Collection Inside Group\n\n```regex\n/(W[0O]W)+/  # One or more of \"WOW\" or \"W0W\"\n```\n\n---\n\n## 8. Capture Groups\n\n### Basic Capture Groups\n\nGroups `()` not only organize patterns but also **capture** matched text for later use.\n\n### File Extension Example\n\nExtract filename without extension:\n\n```regex\n/(.+)\\.(png|jpe?g|pdf)/\n```\n\n**Breakdown:**\n- `(.+)` - **Group 1:** Captures filename (one or more characters)\n- `\\.` - Literal dot\n- `(png|jpe?g|pdf)` - **Group 2:** Captures extension\n  - `png` OR `jp` + optional `e` + `g` OR `pdf`\n\n### Non-Capturing Groups\n\nWhen you need grouping for logic but don't want to capture:\n\n**Syntax:** `(?:)`\n\n```regex\n/(.+)\\.(?:png|jpe?g|pdf)/\n```\n\n- `(.+)` - **Group 1:** Captures filename  \n- `(?:png|jpe?g|pdf)` - Groups for OR logic but doesn't capture\n\n### Numbered Group References\n\nReference captured groups later in the same regex:\n\n**Syntax:** `\\1`, `\\2`, `\\3`, etc.\n\n```regex\n/\u003c(\\w+)\u003e.*?\u003c\\/\\1\u003e/gm\n```\n\n**HTML tag matching breakdown:**\n- `\u003c(\\w+)\u003e` - Opening tag, captures tag name in Group 1\n- `.*?` - Lazy match of content\n- `\u003c\\/\\1\u003e` - Closing tag, references Group 1 (same tag name)\n\n### Named Capture Groups\n\n**Syntax:** `(?\u003cname\u003e)`\n\n```regex\n/(?\u003crootname\u003e.+)\\.(?\u003cextension\u003epng|jpe?g|pdf)/\n```\n\n**Benefits:**\n- More readable than numbers\n- Self-documenting code\n- Easier maintenance\n\n**References:**\n- In regex: `\\k\u003cname\u003e` (some platforms)\n- In replacement: `$\u003cname\u003e` or `${name}`\n\n---\n\n## 9. Substitution (Replace)\n\n### Basic Replacement\n\nReplace matched patterns with fixed strings or references to captured groups.\n\n### Simple Fixed Replacement\n\n```regex\n/kitten|puppy|piglet|foal|fawn|duckling|chick/\n```\n**Replace with:** `\"cutie\"`\n\n### Group References in Replacement\n\n```regex\n/(kitten|puppy|piglet|foal|fawn|duckling|chick)/gm\n```\n**Replace with:** `$1` (references Group 1)\n\n### Remove Characters\n\n```regex\n/[*_]/g\n```\n**Replace with:** `\"\"` (empty string)\n\n### Using Functions in Replacements\n\nInstead of simple strings, use functions for dynamic replacements:\n\n#### 1. Basic Function with Match Parameter\n\n```regex\n/\\b(java|javascript|html|css)\\b/gim\n```\n\n```javascript\nfunction (match) {\n  return match.toUpperCase();\n}\n```\n\n#### 2. Function with Capture Groups\n\n```regex\n/(\\d{3})-(\\d{3})-(\\d{4})/gm\n```\n\n```javascript\nfunction (match, area, prefix, line) {\n  return `+1 (${area}) ${prefix}-${line}`;\n}\n```\n\n**Parameters:**\n- `match` - Full matched text\n- `area`, `prefix`, `line` - Individual captured groups\n\n#### 3. Named Capture Groups in Replacement\n\n```regex\n/(?\u003cmonth\u003e\\d{2})\\/(?\u003cday\u003e\\d{2})\\/(?\u003cyear\u003e\\d{4})/gm\n```\n\n**Replace with:** `\"Date: $\u003cday\u003e-$\u003cmonth\u003e-$\u003cyear\u003e\"`\n\n**Benefits:**\n- More readable than positional references\n- Self-documenting replacements\n- Easier to maintain complex patterns\n\n---\n\n## 10. Lookarounds\n\nLookarounds specify conditions without capturing them as part of the match. They're similar to boundary tokens (`^`, `$`, `\\b`, `\\B`) - they're part of the regex but don't correspond to characters in the match.\n\n### Types of Lookarounds\n\n| Type | Syntax | Description |\n|------|--------|-------------|\n| Positive Lookahead | `(?=)` | Must be followed by |\n| Negative Lookahead | `(?!)` | Must NOT be followed by |\n| Positive Lookbehind | `(?\u003c=)` | Must be preceded by |\n| Negative Lookbehind | `(?\u003c!)` | Must NOT be preceded by |\n\n### Lookahead Examples\n\n#### Negative Lookahead\n```regex\nq(?!u)  # Match 'q' NOT followed by 'u'\n```\n\n#### Positive Lookahead  \n```regex\nq(?=u)  # Match 'q' that IS followed by 'u' (but don't include 'u' in match)\n```\n\n### Lookbehind Examples\n\n#### Negative Lookbehind\n```regex\n(?\u003c!a)b  # Match 'b' NOT preceded by 'a'\n```\n- ✅ Matches 'b' in: \"bed\", \"debt\"  \n- ❌ Doesn't match 'b' in: \"cab\"\n\n#### Positive Lookbehind\n```regex\n(?\u003c=a)b  # Match 'b' that IS preceded by 'a' \n```\n- ✅ Matches 'b' in: \"cab\"\n- ❌ Doesn't match 'b' in: \"bed\", \"debt\"\n\n### Practical Examples\n\n#### USD Price Matching\n```regex\n/(?\u003c=USD )\\d+(?:\\.\\d\\d)?$/mg\n```\n\n**Breakdown:**\n- `(?\u003c=USD )` - Must be preceded by \"USD \" (not included in match)\n- `\\d+` - One or more digits\n- `(?:\\.\\d\\d)?` - Optional non-capturing group for decimal and cents\n- `$` - End of line\n\n#### Capitalized Words (Not Sentence Starters)\n```regex\n/(?\u003c!^)(?\u003c![\\.?!]\"?\\s+\"?)[A-Z]\\w*\\b/\n```\n\n**Breakdown:**\n- `(?\u003c!^)` - NOT at beginning of line\n- `(?\u003c![\\.?!]\"?\\s+\"?)` - NOT preceded by sentence-ending punctuation + optional quotes + whitespace + optional quotes\n- `[A-Z]\\w*\\b` - Capital letter + word characters + word boundary\n\n### Important Notes\n\n#### Dollar Sign in Lookaheads\n```regex\n/\\w{3} \\d\\d(?= 00:00:00 something happened$)/\n```\n\n**Why `$` is inside the lookahead:**\n- Lookaheads don't consume characters\n- If `$` were outside, both the lookahead AND end-of-string would need to come immediately after the match\n- This is impossible since lookaheads don't consume characters\n\n#### Platform Limitations\nSome platforms (like Python) only support **fixed-width** lookarounds:\n- ❌ Variable quantifiers (`*`, `+`, `?`) not allowed\n- ✅ Fixed quantifiers (`{3}`, `{2,4}`) are allowed\n- This prevents performance issues with indeterminate matching\n\n---\n\n### Testing Your Regex\n\n**Recommended Tools:**\n- [RegexPal](https://www.regexpal.com/)\n- [Regex101](https://regex101.com/)\n- [RegExr](https://regexr.com/)\n\n---\n\n## 🤝 Contributing\n\nFound an error or want to improve this guide? \n\n1. Fork the repository\n2. Create your feature branch\n3. Submit a pull request\n\n## 📜 License\n\nThis educational content is provided under the MIT License. Feel free to use, modify, and share!\n\n---\n\n**Happy RegEx-ing! 🚀**\n\n*Remember: Regular expressions are powerful but can be complex. Start simple and build up your patterns gradually.*\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feslamalawy%2Fregex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feslamalawy%2Fregex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feslamalawy%2Fregex/lists"}