---------------------------------------------------------------------------------------- RegExp patterns document. ---------------------------------------------------------------------------------------- OPERATORS ========= | OR + more or equal to one. * zero or more. ? zero or one . single char ^ start $ end COMBINED ======== .* zero or more chars .+ one or more chars .? zero or one SHORTENERS ========== \w a word \b a boundary \d digit(s) \p unicode WHITESPACE ========== \s space \t tab \n lf \r cr REGEXP MODIFIERS ================ g global i case-insensitive m multiline STRING ====== /word/ matches linear string: "word" CONTROL CHARS ============= ---------------------------------------------------------------------------------------- [] indicates INDIVIDUAL or a RANGE of characters. A range is written with semi-colon. []+ more or equal to one occurence []* zero or more occurences []{n,n} specific amount ---------------------------------------------------------------------------------------- [abc] matches individual letters: "a,b,c" [a-z] range, matches a to z [A-Z] range, matches A to Z [0-9] range, matches 0 to 9 [x-z] range, matches x, y, z #[0-9]{3,6}; mixed + range, matches a hexadecimal color code. Example: #001199; ---------------------------------------------------------------------------------------- {} indicates the number of occurences. {0,9} means between 0 and 9. ---------------------------------------------------------------------------------------- () indicates a capture (group), and whole words instead of individual chars: (word|word) ---------------------------------------------------------------------------------------- SHORTENERS ========== Shorteners are quicker ways to accomplish something. Select a whole word: ---------------------------------------------------------------------------------------- Pattern 1: /\w+\(\)/ Pattern 2: /\w{3}/ Pattern 3: /\d+\(\)/ ---------------------------------------------------------------------------------------- Result 1: will match "abc()" in string "function abc()" Result 2: will match "abc" in string "abcdefg" Result 3: will match "123456()" in string "function 123456()" TIP: adding \b before and after it, makes it non-greedy. ---------------------------------------------------------------------------------------- Pattern: /.+\d{1,1}/ ---------------------------------------------------------------------------------------- Result: match "everything" until a digit is encountered: "abcd132xyz" matches: "abcd132" EXAMPLES ======== Below a few practical everyday use examples. NUMBER / DATES ============== Select between a range of dates: ---------------------------------------------------------------------------------------- Pattern: /198[0-9] | 199[0-9] | 200[0-9]/ ---------------------------------------------------------------------------------------- Result: will match everything between 1980 and 2009. Select between a range of dates: ---------------------------------------------------------------------------------------- Pattern: /20[0-9]+/ ---------------------------------------------------------------------------------------- Result: will match everything between 2000 and 2099. Select between a range of dates: ---------------------------------------------------------------------------------------- Pattern: /20[0-9]+|21[0-9]+/ ---------------------------------------------------------------------------------------- Result: will match everything between 2009 and 2100. URLS ==== Domain pattern (non-unicode) ---------------------------------------------------------------------------------------- Pattern: /(http|ftp)[s]*:[\/]+[a-z0-9]+.[a-zA-Z]+.[a-zA-Z]{1,12}/ ---------------------------------------------------------------------------------------- Result: will match https://subdomain.domain.com Result: will match https://www.domain.com Result: will match https://ww2.domain.com LOOKING ======= Looking ahead or behind. String: An apple is red and green and yellow. Pattern 1 look behind: /(red).+(?=green)/ Pattern 2 look ahead:/(?=green).+(yellow)/ Result 1: matches "red", as "green" exists after it. Result 2: matches "yellow", as "green" exists before it.