{"id":20806721,"url":"https://github.com/fed/regex-cheatsheet","last_synced_at":"2026-03-16T10:31:51.561Z","repository":{"id":85048465,"uuid":"129947220","full_name":"fed/regex-cheatsheet","owner":"fed","description":"Regex Cheatsheet 📝","archived":false,"fork":false,"pushed_at":"2018-04-17T18:35:09.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-18T13:33:27.993Z","etag":null,"topics":["cheatsheet","regex"],"latest_commit_sha":null,"homepage":null,"language":null,"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/fed.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}},"created_at":"2018-04-17T18:19:11.000Z","updated_at":"2018-04-17T18:35:10.000Z","dependencies_parsed_at":null,"dependency_job_id":"e63bffb8-b34c-4345-9e94-b40aca2b753f","html_url":"https://github.com/fed/regex-cheatsheet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fed%2Fregex-cheatsheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fed%2Fregex-cheatsheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fed%2Fregex-cheatsheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fed%2Fregex-cheatsheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fed","download_url":"https://codeload.github.com/fed/regex-cheatsheet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243152875,"owners_count":20244657,"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":["cheatsheet","regex"],"created_at":"2024-11-17T19:24:54.051Z","updated_at":"2025-12-24T10:42:25.929Z","avatar_url":"https://github.com/fed.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Regular Expressions\n\nWe can use regular expressions for both matching and capturing values.\n\n## Matching values\n\n`.` matches **any character or sequence of characters** (except line breaks).\n\n---\n\n`?` marks the preceding character as **optional**: matches between 0 and 1 of the preceding token. e.g.:\n\n`/Hello?/` matches both `Hell` and `Hello`.\n`/He(llo)?/` matches both `He` and `Hello` but not `Hell`.\n\n---\n\n`g`: Global search flag, once it finds an occurrence it keeps looking for more matches (if the flag is ON) or just stops there (if OFF).\n\n`/Hello/` will match any number of occurrences.\n\n---\n\n`\\`: escaping character, if i need to match a period instead of using the wildcard, I need to escape it as follows:\n\n`/\\./` will only match a period.\n`/./` will match any character or sequence of characters (wildcard).\n\n---\n\n`[]`: character classes, use a character class to specify a list of characters to match ONE of, e.g.\n\n`/gr[ae]y/` will match both `grey` and `grey`.\n\n---\n\n`+`, plus symbol: matches one or more of the preceding token. e.g.\n\n`/g+/` we will be matching as many `g`'s as possible: both `gg` and `gggggggg`.\n\n**Greedy search:** the `+` symbol is greedy, meaning it's gonna try to match as much as it can, e.g.\n\n`/H.+H/` matches `Hello world H` but also `Hello world Hello H`.\n\nThis means it's gonna try to match as many capital Hs as it possibly can until there are no more remaining.\n\n**Don't be greedy:** as soon as you find an `H` (in the previous example) cut off, you are done. You can achieve this by doing: `/H.+?H/`.\n\n---\n\n`*`, star symbol: will match zero or more occurrences of the preceding token.\n\n---\n\n`|`, alternation symbol: the pipe is the equivalent to the logical OR\n\n`/gr(a|e)y\\` will match both `grey` and `grey`.\n`/(https?|ftp):\\/\\//.+` matches either `http(s)` or `ftp` followed by any single character repeated one or more times.\n\n---\n\n`^`, caret symbol: matches the beginning of a string.\n\n---\n\n`$` matches the end of the string. Limits what we are searching for (useful for file extensions). e.g.\n\n`/\\.jpg$/` matches `.jpg` only when it occurs at the end of the string.\n\n---\n\n`m`, multiline flag: when we are working with the beginning and the end of the string, we want to make sure this flag is ON.\n\n`/^H/gm`\n\n---\n\n`\\d` means any digit from 0 to 9.\n\nYou can also pass in some arguments:\n\n* `/\\d{3}/` to match 3 occurrences of the preceding token.\n* `/\\d{3,4}/` where 3 is the minimum and 4 the maximum.\n\n---\n\n`\\w` matches any letter. This equals: `/[a-zA-Z]/` which looks for any lowercase a to z or any uppercase A to Z.\n\n---\n\n`\\s` matches line breaks, spaces and tabs.\n\n---\n\n* `\\r` = carriage return\n* `\\n` = new lines\n* `\\s` = space characters\n* `\\w` = word character\n* `\\d` = digits 0-9\n\n---\n\n`[]` = character class\n\n`/[^abc]/` means: look for any character that is NOT a, b or c.\n\n## Capturing values\n\nConsider the following example:\n\n```\n/(^.+(jpg|gif|png)$)/\n |   |_____v_____| |\n |        $2       |\n |________v________|\n          $1\n```\n\n`$x` always corresponds to whatever was within **parenthesis**.\n\n* `$1` will match the whole filename.\n* `$2` will match the file extension.\n\n**Note that it starts from the outside and it works its was in.**\n\nIf we are just using parenthesis for boundaries and don't need to match whatever is inside, use `?:` which means DO NOT CAPTURE.\n\n**Example: Search and Replace.** Given:\n\n```\n\u003cpre class=\"html\" name=\"code\"\u003e\n```\n\nFind:\n\n```\n\u003cpre class=\"([^\"]+)\" name=\"code\"\u003e\n```\n\nThis means: anything that is not a closing quotation mark repeating one or more times.\n\nReplace with: `[$1]`.\n\nSearch and replace in PHP: `preg_replace`.\n\n# Regex Cheatsheet\n\n* Good tester: http://www.regexr.com/\n* Cheat sheet: http://ole.michelsen.dk/tools/regex.html\n\n```\n| = or\n(ab|ba)\n() = capture\n(Java|Ecma)Script\n(?:Java|Ecma)Script =\u003e don't care about capture\n\n// negated character classes\n[^g-z]\n// shortcuts\n\\W = [^\\w]\n\\D = [^\\d]\n\\S = [^\\s]\n\n/#\\w{3,6}/g = hex\n#123ABC\n#abc\n\nhello6jkhkjhkjh987\n2020-01-32\n\nRanges\n[\\w-] = word characters + hyphens\n\\s = whitespace =~[\\t\\r\\n]\n\\d = [0-9] (not decimals)\n\n// no look behind in javascipt (does not effect match position\n// look ahead - when not followed by\na(?!b)\nab ac\n// look ahead\n(what is want a that is followed by a b, but do not match the b)\nlook aheads can contain capture groups\na(?=b)\nab\n\n\\B - non-word boundary = between \\w and \\w or \\W and \\W\n\\b = boundary (useful for checking if string is in list) (between \\w and \\W)\n/^ = beginning of line\n$/ = end of line\n/^hello$/ = match only hello on a line (use /m for multi line)\n\nhello\n[-+]?\\d*(?:\\.\\d+)?\n15\n134.2020\n-.2000\n.293\n20\n\n\n\\w = [a-zA-Z0-9_]\n[a-z]\n[a-z0-9_]\n// most characters do not need escaping in range\n\nSets\n[abc]\n[abc]+ // 1 or more of any of thses\n\n// regex = greey -\u003e '?' turns off greedyness\na{0,1} a? =\u003e 0 to 1\na{1,} a+ =\u003e 1 to many\na{0,} a* =\u003e 0 to many\naaaaaaaaaaaa (a{12})\n{} = quatifier\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffed%2Fregex-cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffed%2Fregex-cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffed%2Fregex-cheatsheet/lists"}