{"id":19565062,"url":"https://github.com/erikplachta/cs-regex-reference-guide","last_synced_at":"2026-03-04T06:04:03.065Z","repository":{"id":119719353,"uuid":"464194950","full_name":"ErikPlachta/cs-regex-reference-guide","owner":"ErikPlachta","description":"A general reference guide on how to understand and use regex.","archived":false,"fork":false,"pushed_at":"2022-03-12T15:17:22.000Z","size":79,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-26T09:28:00.177Z","etag":null,"topics":["computer-science","cs","guide","reference","regex","regular-expression","regular-expressions"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ErikPlachta.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2022-02-27T16:46:44.000Z","updated_at":"2022-11-16T01:54:49.000Z","dependencies_parsed_at":null,"dependency_job_id":"a6f3ec0e-b194-483f-95ce-48b2ce8ad27a","html_url":"https://github.com/ErikPlachta/cs-regex-reference-guide","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ErikPlachta/cs-regex-reference-guide","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ErikPlachta%2Fcs-regex-reference-guide","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ErikPlachta%2Fcs-regex-reference-guide/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ErikPlachta%2Fcs-regex-reference-guide/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ErikPlachta%2Fcs-regex-reference-guide/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ErikPlachta","download_url":"https://codeload.github.com/ErikPlachta/cs-regex-reference-guide/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ErikPlachta%2Fcs-regex-reference-guide/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30073706,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-04T05:31:57.858Z","status":"ssl_error","status_checked_at":"2026-03-04T05:31:38.462Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["computer-science","cs","guide","reference","regex","regular-expression","regular-expressions"],"created_at":"2024-11-11T05:24:53.963Z","updated_at":"2026-03-04T06:04:03.041Z","avatar_url":"https://github.com/ErikPlachta.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# CS Regex Reference Guide for JavaScript\n\nCheck out this Gist if you're interested in learning more about Regex, aka\nRegular Expressions.\n\u003e It's not a complete guide, but I did cover the basics to help you get started.\n\u003e I've also included my references and contact information at the bottom if you\n\u003e want to learn more.\n\n---\n\n## Summary\n\nThis is a general reference guide on how to understand and use some basic regular\nexpressions. I've broken down specific functions below with simple examples to\nhelp you learn the concepts quickly.\n\n---\n\n**Publish Notes**\nThe content on this Gist was created on **[this GitHub Repo](https://github.com/ErikPlachta/cs-regex-reference-guide/)**, published to this **[GitHub Website](https://erikplachta.github.io/cs-regex-reference-guide/)**, and published on [this GitHub Gist](https://gist.github.com/ErikPlachta/250107ea2e00086af9c1d29082c502b1/).\n\n---\n\n---\n\n## Repo Stats\n\n[![GitHub license](https://img.shields.io/github/license/ErikPlachta/cs-regex-reference-guide)](https://github.com/ErikPlachta/cs-regex-reference-guide) [![GitHub Number of Languages](https://img.shields.io/github/languages/count/ErikPlachta/cs-regex-reference-guide)](https://github.com/ErikPlachta/cs-regex-reference-guide)\n[![GitHub top Language](https://img.shields.io/github/languages/top/ErikPlachta/cs-regex-reference-guide)](https://github.com/ErikPlachta/cs-regex-reference-guide)\n[![GitHub issues](https://img.shields.io/github/issues/ErikPlachta/cs-regex-reference-guide)](https://github.com/ErikPlachta/cs-regex-reference-guide/issues)\n![GitHub last commit](https://img.shields.io/github/last-commit/erikplachta/cs-regex-reference-guide)\n\n---\n\n---\n\n## Table of Contents\n\n- [CS Regex Reference Guide for JavaScript](#cs-regex-reference-guide-for-javascript)\n  - [Summary](#summary)\n  - [Repo Stats](#repo-stats)\n  - [Table of Contents](#table-of-contents)\n  - [1. What is Regex?](#1-what-is-regex)\n    - [What are some other ways to explain Regular Expressions?](#what-are-some-other-ways-to-explain-regular-expressions)\n  - [2. Starting with Examples](#2-starting-with-examples)\n    - [Understanding Regex -\u003e Regular Expressions Are ~~not~~ Easy to Understand](#undersatnding-regex---regular-expressions-are-not-easy-to-understand)\n    - [**Example - Phone Number**](#example---phone-number)\n    - [**Example - Email Address**](#example---email-address)\n  - [The Syntax](#the-syntax)\n    - [**1. Regex Components**](#1-regex-components)\n      - [**1.1 Literal Characters**](#11-literal-characters)\n      - [**1.2 Meta Characters**](#12-meta-characters)\n    - [**2. Anchors / Positions**](#2-anchors--positions)\n    - [**3. Quantifiers -  Greedy and Lazy Match**](#3-quantifiers----greedy-and-lazy-match)\n    - [**4. OR Operators**](#4-or-operators)\n      - [**4.1 Character Classes / Bracket Expressions**](#41-character-classes--bracket-expressions)\n      - [**4.2 Alteration Classes / Grouping and Capturing**](#42-alteration-classes--grouping-and-capturing)\n    - [**5. Boundaries**](#5-boundaries)\n    - [**6. Flags**](#6-flags)\n    - [**7. Back-references**](#7-back-references)\n    - [**8. Look-ahead and Look-behind**](#8-look-ahead-and-look-behind)\n  - [Author](#author)\n  - [Contact Me](#contact-me)\n  - [Resources and References](#resources-and-references)\n\n---\n\n---\n\n## 1. What is Regex?\n\n... aka **regular expression**, is a universal syntax language used to simplify\nadvanced searching/filtering of content based on a user-specified search pattern.\n\u003e You define what you are searching at the level of precision you need.\n\n**What makes a regex search/filter different from others is that it searches for\npatterns in ASCII or Unicode characters.**\n\u003e You're not just looking for a specific character value, you're looking for all\n\u003e instances of a pattern within the content. For example, all phone-numbers,\n\u003e email-addresses, websites, or really any type of content that follows a\n\u003e universal pattern.\n\n---\n\n---\n\n### What are some other ways to explain Regular Expressions?\n\nGreat questions!\n\nThe [MDN team said,](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)\n\u003e \"*Regular expressions are patterns used to match character combinations in strings.*\"\n\n[Wikipedia says,](https://en.wikipedia.org/wiki/Regular_expression)\n\u003e \"*A regular expression is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for \"find\" or \"find and replace\" operations on strings, or for input validation.*\"\n\n---\n\n---\n\n## 2. Starting with Examples\n\n### Understanding Regex -\u003e Regular Expressions Are ~~not~~ Easy to Understand\n\nTo understand a regex pattern, *the search / filter you're creating*, you'll need\nto learn some syntax. But first, let's start with some examples.\n\n### **Example - Phone Number**\n\nWithout the area-code, phone numbers are generally 10-digits separated by a space\nor a hyphen.\n\n---\n\n**We can look for 10 digits like this `/\\d\\d\\d-\\d\\d\\d-\\d\\d\\d\\d/`**\nHere's, we're using the [meta character](#2-meta-characters) argument `\\d` for \neach unique digit that we're searching for separated by a hyphen.\n\nBUT what IF the single phone-number is formatted differently through-out the data?\nFor Example, our regex expression ran on the below data would only return 1\nresults, `123-456-7890`, even though ALL of them are the same phone number.\n\u003e `(123)-456-7890`, `123-456-7890`, `123.456.7890`, and `123 456 7890`\n\n---\n\n**So how could we improve our regex expression?**\n\nWell, considering the same phone numbers 4 times again, we want to account for\n`(`,`)`, ` `, and `-`.\n\u003e `(123)-456-7890`, `123-456-7890`, `123.456.7890`, and `123 456 7890`\n\n**1.** Add a FEW OR operators to account for spaces vs hyphens\n    \u003e To do this we'll use **`[]`**, the [character class syntax](#character-classes),\n    \u003e where each [literal character](#1-literal-characters) inside the square-\n    \u003e brackets is considered a unique argument.\n\n**2.** Add the ability to match `(` and `)` if they exist.\n    \u003e For optional parameters, we'll use the **`?`** [quantifier](#quantifiers),\n    \u003e which allows us to search for instances that a value does and does not exist.\n\n**What does this fully fleshed out syntactically accurate regex argument look like?**\n\n`/(\\(?)+(\\d{3})+[-.) ]+(\\s?)(\\d{3})+[-. ]+(\\s?)+(\\d{4})/`\n\nLet's break it down 👇🏼\n\n| Syntax      | Description |\n|-------------|-------------|\n| **`/`** | Starting the regex expression |\n| **`\\(?`** | Left-parenthesis `(` if exists|\n| **`+`** | Followed by... |\n| **`\\d{3}`** | A collection of 3 digits |\n| **`+`** | Followed by... |\n| **`[)-. ]`** | a right-parenthesis `)`, OR hyphen `-`, OR period `.`, OR a space` `|\n| **`+`** | Followed by... |\n| **`\\s?`** | A white-space if it exists |\n| **`[- ]`** | Hyphen OR a space|\n| **`+`** | Followed by... |\n| **`\\d{3}`** | A collection of 3 digits |\n| **`+`** | Followed by... |\n| **`[-. ]`** | A hyphen, dash, or space |\n| **`+`** | Followed by... |\n| **`\\s?`** | A white-space if it exists |\n| **`+`** | Followed by... |\n| **`\\d{4}`** | A collection of 4 digits |\n| **`/`** | Ending the regex expression |\n\n---\n\n### **Example - Email Address**\n\nNow that we've covered the basics, let's look at a regex search pattern built to\nsearch for email addresses.\n\u003e You'll notice I've covered less details here.\n\n**Do you see a pattern in this regex search pattern?**\n\n`/([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})/`\n\nLet's break it apart into smaller chunks based on the high-level patterns we see!\n\n| Symbol | Description |\n|--------|-------------|\n| **`/`** | Starting the regex expression |\n|**`(`** | Encapsulating a sub-expression |\n|**`[a-z0-9_\\.-]`** | Any alpha-numeric characters along with `_`, `.`, and `-`. Results must |\n|**`+)`** | Ending sub-expression, and requiring it be followed by the next argument. |\n|**`@`**    | MUST include the `@` character. |\n|**`([\\da-z\\.-]+)`** | Any digit or alpha-number character, followed by a `.` or `-`. |\n|**`([a-z\\.]{2,6})`** | Look for alpha-characters of any combination between 2-6 characters in length |\n| **`/`** | Ending the regex expression |\n\n---\n\n---\n\n## The Syntax\n\nIn this section, I've included the syntax used in the above examples. The goal\nis to have it server as a point-of-reference for the above along with helping you\ndevelop the confidence and knowledge to create your own regex patterns.\n\u003e Not all sections contain examples by design.\n\n\n---\n\n\u003e *Note: If you're looking for a resource to make it easy to lean regex while\n\u003e following along with this guide, check out this  website https://regexr.com/.*\n\u003e\u003e *It allows you to create regex search patterns and get live-feedback.*\n\n---\n\n### **1. Regex Components**\n\n#### **1.1 Literal Characters**\n\nAny/all ASCII or Unicode characters you're wanting to search for or filter out. \n\u003e This will include single characters.\n\n| Syntax | Description                |\n|--------|----------------------------|\n| **`a-z`** | Any lower-case letters  |\n| **`A-Z`** | Any upper-case letters  |\n| **`0-9`** | Any digits |\n| **`\\.`**  | A period character |\n| **`Unicode Characters`**  | There's a lot. here's an index -\u003e *[Microsoft - Insert ASCII or Unicode Latin-based symbols and characters](https://support.microsoft.com/en-gb/office/insert-ascii-or-unicode-latin-based-symbols-and-characters-d13f58d3-7bcb-44a7-a4d5-972ee12e50e0)* |\n\n#### **1.2 Meta Characters**\n\nRegex operator that represent specific data-types within the ASCI or Unicode\ncharacter sets.\n\n| Syntax | Description  | Note | Example |\n|--------|--------------|------|---------|\n| **`\\`** | Converts qualifying ASCI or Unicode character into meta-characters. | *WARNING: If you don't use this it will be considered a literal-character!* | |\n| **`/^`**| Any new line | | |\n| **`.`** | Any ASCI or Unicode Character.| *WARNING: Within a [character class](#character-classes), a `.` does not need to be escaped to be read as a [literal character](#1-literal-characters)* | |\n| **`\\d`**| Any Digit 0-9 | | |\n| **`\\w`**| Anything that is a word-character | *`A-Z`*, *`a-z`*, **`0-9`** |  **`\\w\\w`** -\u003e Returns any sequent of two word-characters. |\n| **`\\W`**| Anything that is NOT a word-character |   |   |\n| **`\\s`**| Any white-space characters. | `Space`, `Tab`, and sometimes `new-line`| **`\\s\\s`** -\u003e Returns any sequent of two white-space characters. |\n| **`\\S`**| Anything that is NOT white-space. | `Space`, `Tab`, and sometimes `new-line` | |\n| **`[a-z]`**| All character a-z. | When in a class, the `-` plays as an operator to reutrn a-z character argument values. See the [character classes](#character-classes) section for more details | | `[a-c]` -\u003e return all characters between `a` and `c` |\n\n---\n\n---\n\n### **2. Anchors / Positions**\n\n... are used to match the location of a literal character within your defined\nsearch parameters.\n\n| Syntax  | Description  |   Example   |\n|---------|--------------|-------------|\n| **`^`** | Used to look for a string value start | `^test` looks for all strings that start with the [literal characters](#1-literal-characters) `t`, `e`, `s`, and `t`. |\n| **`$`** | Used to look for [literal characters](#1-literal-characters) that end with a specific value. | `/test$/` looks for all strings that end with the [literal characters](#1-literal-characters) `t`, `e`, `s`, and `t`. |\n\n---\n\n---\n\n### **3. Quantifiers -  Greedy and Lazy Match**\n\n... are a meta character that modify the pervious meta characters in a regular expression.\n\u003e Based on your regex search parameters, how many of times do you want it to\n\u003e match in a row?\n\n| Syntax | Description  |   Example   |\n|--------|--------------|-------------|\n| **`*`**|  0 or more   | **`/\\d*/`** -\u003e returns all digits, period. |\n| **`?`**|  0 or 1      | **`/test?t/`** -\u003e all combinations of `test` and `testt` where the second T is optional. |\n| **`+`**|  1 or more   | **`/\\d+/`** -\u003e returns all digits, of length 1 or more. | |\n| **`{min,max}`**| Range of number of times former argument must exist to qualify as a result. | **`\\w{1,5}`** -\u003e All word-character combinations with 1-5 characters followed by white-space.|\n| **`{n}`**| Number of times the former argument must exist to qualify as a result. | **`\\w{5}\\s`** -\u003e All word-character combinations with 5 character followed by white-space.   |\n\n---\n\n---\n\n### **4. OR Operators**\n\nHow to use OR arguments within a regex statement.\n\n#### **4.1 Character Classes / Bracket Expressions**\n\n... is one of the two **OR operators**, where arguments are placed inside of square-brackets **`[ ]`**.\n\n| Syntax | Description | Notes | Example |\n|--------|-------------|-------|---------|\n| **`[^argument]`** | **NOT OR Operator**, returns anything except for the argument in the Class | A carrot, `^`, becomes a [meta character](#2-meta-characters) when used at the preface of a class. Anywhere else and it becomes a [literal character](#1-literal-characters) | `[^0-5]` -\u003e anything not 0-5. `[^a-c]` -\u003e Anything that is not between the letters a-c.\n| **`[.]`** |  All literal character instances of a period, `.` | Within a class, does **not** need to be escaped to be read as a literal character. |`[-.]` -\u003e looks for the literal characters `-` OR `.` |\n| **`a[bc]de`** | All cases of `abde` AND/OR `acde`. |   |  |\n| **`[letter-letter]`**   | Any literal characters a-z based on character case. | A hyphen, `-` becomes a [meta character](#2-meta-characters) when used between two literal characters of the same family within a class. | `[a-c]` -\u003e return all characters between `a` and `c`. `/\\b[A-Za-z]{4}\\b/` -\u003e to match any 4-letter word with letter [literal characters](#1-literal-characters) in it. `/\\b[A-Z][a-z]*\\b/` -\u003e to match any 0-or more letter word with letter [literal characters](#1-literal-characters) in it starting with a capital letter. `\\b[\\w]{4}\\b` -\u003e All 4 letter words that contain any value used within words. |\n| **`[number-number]`** | A range between two numbers | A hyphen, `-` becomes a [meta character](#2-meta-characters) when used between two literal characters of the same family within a class. | `[0-5]{3}` -\u003e All combinations of 3-digits where each unique digit is between 0 - 5 |\n\n---\n\n#### **4.2 Alteration Classes / Grouping and Capturing**\n\n... is the second **OR Operator**, and is used with as an or operator to look\nfor grouped literal characters within parenthesis and separated by a vertical\nbar `( arg1 | arg2 )`.\n\n| Syntax | Description | Notes | Example |\n|--------|-------------|-------|---------|\n| **`(arg1\\|arg2)`** | Return all instances where `arg1` or `arg2` exist. | This is how you search for very specific groups of literal characters. | `/[\\w.]+@\\w+\\.+(com\\|net\\|edu)/` -\u003e Returns all email address that end with .net, .com, or.edu |\n\n---\n\n---\n\n### **5. Boundaries**\n\n| Syntax  | Description  |   Example   |\n|---------|--------------|-------------|\n| **`\\b`** |  A word boundry | **All 4 letter words** -\u003e **`/\\b\\w{4}/`** -\u003e Looking at each word, look ALL word-character values of length 4. `/\\btest\\b/` -\u003e Returns a whole word search |\n\n---\n\n---\n\n### **6. Flags**\n\n... are used to classify specific search-case scenarios to you regex expression.\nThey can be combined or used individually as needed, and are added to the end\nof your regex expression. `/regex-pattern/flag`\n\n| Syntax  | Description  |   Example   |\n|---------|--------------|-------------|\n| **`g`** | Globally searching. |`/[a-z]/g` -\u003e returns all letters within all content. |\n| **`i`** | Case-insensitive searching | `/[a-z]/gi` -\u003e returns ALL literal character despite case from A-Z and a-z |\n| **`m`** | Multi-line searching. | `/^\\d/gm` -\u003e returns ALL initial digits within all lines \n| **`s`** | `Dotall mode` returns results with any [literal character](#11-literal-characters) between them. | |\n| **`u`** | Enable Unicode support | |\n| **`y`** | Sticky mode allows you to search exact position within content | |\n\n---\n\n---\n\n### **7. Back-references**\n\n… are used to synchronize pattern-group result parameters within a regex expression based on the pattern group you specify.\n\n| Syntax  | Description  |   Example   |\n|---------|--------------|-------------|\n| **`\\n`** | References `n` pattern group for what parameters to look for. | **`(['\"])(.*?)\\1`** ran on the content \\``Testing: \"my regex expression...\"`\\` -\u003e returns `\"my regex expression...\"`.\n\n---\n\n---\n\n### **8. Look-ahead and Look-behind**\n\n| Syntax  | Description  |\n|---------|--------------|\n| **`(?=arg)`**\t  | Lookahead returns what's immediately after `arg` |\n| **`(?\u003c=arg)`**\t| Lookbehind returns what's immediately before `arg` |\n\n---\n\n---\n\n\u003c!--\n![GitHub commit activity](https://img.shields.io/github/commit-activity/w/erikplachta/cs-regex-reference-guide)\n![GitHub commit activity](https://img.shields.io/github/commit-activity/m/erikplachta/cs-regex-reference-guide)\n![GitHub commit activity](https://img.shields.io/github/commit-activity/y/erikplachta/cs-regex-reference-guide)\n--\u003e\n## Author\n\n**[Erik Plachta](https://github.com/ErikPlachta)**\n\n**Thanks for taking the time to read this!**\n\u003e If you want to check out more of my work, head on over to my [GitHub Page](https://www.github.com/erikplachta).\n\n## Contact Me\n\n**Do you want to get in touch?**\n\u003e Feel free to connect with me on my [Twitter @ErikPlachta](https://www.twitter.com/erikplachta/) or [LinkedIn @ErikPlachta](https://www.linkedin.com/in/erikplachta/)\n\n---\n\n## Resources and References\n\nA collection of resources I used to learn about Regex.\n\n- [Wikipedia - Regular Expressions](https://en.wikipedia.org/wiki/Regular_expression)\n- [MDN - Regular Expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)\n- [Microsoft - Regular Expression Language - Quick Reference](https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference)\n- [MDN - Regular expression syntax cheat sheet](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Cheatsheet)\n- [Regular-Exprsesions.info](https://www.regular-expressions.info/)\n- [RexEgg.com](https://www.rexegg.com/)\n- [YouTube - The Coding Train - Introduction to Regular Expressions - Programming with Text](https://www.youtube.com/watch?v=7DG3kCDx53c)\n- [Regex tutorial — A quick cheat sheet by examples](https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285)\n- [zone.ni.com - Regular Expressions Components](https://zone.ni.com/reference/en-XX/help/371714F-01/nirghelp/regular_expressions_components/)\n- [javascript.info - Backreferences in pattern](https://javascript.info/regexp-backreferences)\n- [rexegg.com - Mastering Lookahead and Lookbehind](https://www.rexegg.com/regex-lookarounds.html)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferikplachta%2Fcs-regex-reference-guide","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ferikplachta%2Fcs-regex-reference-guide","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferikplachta%2Fcs-regex-reference-guide/lists"}