Skip to content

utkuufuk/regexref

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 

Repository files navigation

RegEx Reference

  1. Tools
  2. Cheatsheet
  3. Examples
  4. Programming Languages

Tools

  1. Online Tool

  2. grep -P "<regex>" <path> # omit leading & trailing '/' from regex
  3. sag "<regex>" <path> # omit leading & trailing '/' from regex
  4. VS Code: Ctrl+F > Alt+R

Cheatsheet

Common

RegEx Description
. Any Character Except New Line
\d Digit (0-9)
\D Not a Digit (0-9)
\w Word Character (a-z, A-Z, 0-9, _)
\W Not a Word Character
\s Whitespace (space, tab, newline)
\S Not Whitespace (space, tab, newline)
\b Word Boundary
\B Not a Word Boundary
^ Beginning of a String
$ End of a String
[] Matches Characters in brackets
[^ ] Matches Characters NOT in brackets
( ) Either Or Group

Quantifiers

RegEx Description
* 0 or More
+ 1 or More
? 0 or One
{3} Exact Number
{3,4} Range of Numbers (Minimum, Maximum)

Examples

Exact Match

ninja       # 'ninja'
^ninja$     # exactly 'ninja' and nothing else

Character Set

[ng]inja    # 'ninja' or 'ginja'
ninja[1-9]  # 'ninja1' to 'ninja9'
[^n]inja    # every '_inja' except 'ninja'

Multiple Characters

[0-9]+          # multiple digits
a{3}            # 3 'a's
[a-zA-Z]{5,8}   # 5 to 8 characters

Optional/Alternate Matches

(cat|dog|bird)  # 'cat' or 'dog' or 'bird'
M(r|s|rs)\.?    # 'Mr/Mr./Ms/Ms./Mrs/Mrs.'

Programming Languages

JavaScript

// these two are equivalent
const reg1 = /[a-z]/i
const reg2 = new RegExp(/[a-z]/, 'i')

reg1.test("b")  // true
reg2.test("3")  // false

Matching Groups

const text = "http://www.google.com";
const regex = /https?://(www\.)?(\w+)(\.\w+)/
const [whole, www, domain, extension] = text.match(regex)

About

Regex reference sheet

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published