|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | +# Cheat Sheet |
| 5 | + |
| 6 | +`.test()` takes the regex, applies it to a string, and returns a boolean. |
| 7 | +`.replace()` replaces a substring with another substring and returns the modified string.`.match()` extracts the actual matches you found in the return array. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +## Match literal strings |
| 12 | + |
| 13 | +`/test/` searches for a literal match of the string. |
| 14 | +``` |
| 15 | +const str = 'test'; |
| 16 | +const reg = /test/; |
| 17 | +reg.test(str); // return true |
| 18 | +``` |
| 19 | +## OR (`|`) in literal strings |
| 20 | + |
| 21 | +Search for multiple patterns using the OR operator `|`. |
| 22 | +``` |
| 23 | +const str = 'second'; |
| 24 | +const reg = /first|second/; |
| 25 | +reg.test(str); // return true |
| 26 | +``` |
| 27 | +## Ignore case (`i`) |
| 28 | + |
| 29 | +Use `i` to search ignore-case. |
| 30 | +``` |
| 31 | +const str = 'firstClass'; |
| 32 | +const reg = /firstclass/i; |
| 33 | +reg.test(str); // return true |
| 34 | +``` |
| 35 | +## Use global (`g`) |
| 36 | + |
| 37 | +Use `g` to search or extract a pattern more than once. |
| 38 | +``` |
| 39 | +const str = 'test test'; |
| 40 | +const reg = /test/g; |
| 41 | +str.match(reg); // return ['test', 'test'] |
| 42 | +``` |
| 43 | +## Match anything with wildcard period (wildcard, dot, period) |
| 44 | + |
| 45 | +The wildcard character `.` will match any one character. |
| 46 | +``` |
| 47 | +const str = 'The string is strong'; |
| 48 | +const reg = /str./; |
| 49 | +str.match(reg); // return ['stri'] |
| 50 | +``` |
| 51 | +## Match a single character with multiple possibilities [ ] |
| 52 | + |
| 53 | +Search for a literal pattern with some flexibility with character classes by using `/[abei]/`. |
| 54 | +``` |
| 55 | +const str = 'The tree is strong'; |
| 56 | +const reg = /t[hr]e/gi; |
| 57 | +str.match(reg); // ['The', 'tre'] |
| 58 | +``` |
| 59 | +## Match letters of the alphabet |
| 60 | + |
| 61 | +Search for an alphabet literal pattern by using `[a-z]`. |
| 62 | +``` |
| 63 | +const str = 'The'; |
| 64 | +const reg = /[a-z]+/; |
| 65 | +str.match(reg); // ['he'] |
| 66 | +``` |
| 67 | +## Match single characters not specified |
| 68 | + |
| 69 | +Use `[^ae]` for sets of characters that you do not want to match. |
| 70 | +``` |
| 71 | +const str = 'Test'; |
| 72 | +const reg = /[^ae]/; |
| 73 | +reg.test(str); // true |
| 74 | +``` |
| 75 | +## Match characters that occur one or more times |
| 76 | + |
| 77 | +Use `**+**` to match characters that occur at least once and may be repeated. |
| 78 | +``` |
| 79 | +const str = 'the text, the apple'; |
| 80 | +const reg = /the+/g; |
| 81 | +str.match(reg); // ['the', 'the'] |
| 82 | +``` |
| 83 | +## Match characters that occur zero or more times |
| 84 | + |
| 85 | +The asterisk `*` matches characters that occur zero or more times. |
| 86 | +``` |
| 87 | +const str = 'greedy'; |
| 88 | +const reg = /e*/; |
| 89 | +reg.test(str); // true |
| 90 | +``` |
| 91 | +## Match beginning string patterns |
| 92 | + |
| 93 | +The caret `**^**` is used to search for patterns at the beginning of strings. |
| 94 | +``` |
| 95 | +const str = 'Test'; |
| 96 | +const reg = /^A/i; |
| 97 | +reg.test(str); // false |
| 98 | +``` |
| 99 | +## Match ending string patterns (`$`) |
| 100 | + |
| 101 | +Use `$` to search for patterns at the end of strings. |
| 102 | +``` |
| 103 | +const str = 'test-id-sDb4r'; |
| 104 | +const reg = /-\w{5}$/; |
| 105 | +const result = str.replace(reg, ''); // 'test-id' |
| 106 | +``` |
| 107 | +## Match all letters and numbers |
| 108 | + |
| 109 | +Use `\w` as shorthand for character classes that are equal to `/[A-Za-z0–9_]/`. Include the underscore (`_`) character. |
| 110 | +``` |
| 111 | +const str = 'sDb4r'; |
| 112 | +const reg = /\w/; |
| 113 | +reg.test(str); // true |
| 114 | +``` |
| 115 | +## Match everything but letters and numbers |
| 116 | + |
| 117 | +`\W` search is the opposite of alphanumerics and is equal to `/[^A-Za-z0–9_]/`. |
| 118 | +``` |
| 119 | +const str = 'sDb4r?'; |
| 120 | +const reg = /\W/; |
| 121 | +str.match(reg); // ['?'] |
| 122 | +``` |
| 123 | +## Match all numbers /d |
| 124 | + |
| 125 | +`/d` is used to look for digit characters and is equal to `/[0–9]/`. |
| 126 | +``` |
| 127 | +const str = 'sDb4r?'; |
| 128 | +const reg = /\d/; |
| 129 | +str.match(reg); // ['4'] |
| 130 | +``` |
| 131 | +## Match all non-numbers |
| 132 | + |
| 133 | +`/D` is used to look for non-digit characters and is equal to `/[^0-9]/`. |
| 134 | +``` |
| 135 | +const str = 'sDb4r?'; |
| 136 | +const reg = /\D/g; |
| 137 | +str.match(reg); //['s', 'D', 'b', 'r', '?'] |
| 138 | +``` |
| 139 | +## Match whitespace `\s` |
| 140 | + |
| 141 | +Search for whitespace using `\s`. This pattern not only matches whitespace but also carriage return, tab, form feed, and new line characters. It’s similar to `\r\t\f\n\v`. |
| 142 | + |
| 143 | +``` |
| 144 | +const str = 'test string'; |
| 145 | +const reg = /\s/; |
| 146 | +str.replace(reg, ' new '); // 'test new string' |
| 147 | +``` |
| 148 | +## Match non-whitespace characters `\S` |
| 149 | + |
| 150 | +Search for non-whitespace using `\S`. This pattern will not match whitespace, carriage return, tab, form feed, and new line characters. It’s similar to `^ \r\t\f\n\v`. |
| 151 | + |
| 152 | +``` |
| 153 | +const str = 'test string'; |
| 154 | +const reg = /test\S/; |
| 155 | +reg.test(str); // false |
| 156 | +``` |
| 157 | +## Find characters with lazy matching (`?`) |
| 158 | + |
| 159 | +`?` finds the smallest possible part of the string that satisfies the regex pattern. |
| 160 | + |
| 161 | +``` |
| 162 | +const str = '<h1>Title</h1>'; |
| 163 | +const reg = /<.*?>/; |
| 164 | +str.match(reg); // ['<h1>'] |
| 165 | +``` |
| 166 | +## Specify the upper and lower number of matches |
| 167 | + |
| 168 | +You can specify the lower and upper number of patterns with quantity specifiers. Quantity specifiers are used with curly brackets: ({ }). You put two numbers between the curly brackets for the lower and upper number of patterns. |
| 169 | + |
| 170 | +``` |
| 171 | +const str = 'google'; |
| 172 | +const str1 = 'gooogle'; |
| 173 | +const str2 = 'gogle'; |
| 174 | +const reg = /go{2,3}gle/; |
| 175 | +reg.test(str); // true |
| 176 | +reg.test(str2); // false |
| 177 | +reg.test(str1); // true |
| 178 | +``` |
| 179 | +## Specify only the lower number of matches |
| 180 | + |
| 181 | +To specify the lower number of patterns, keep the first number followed by a comma. |
| 182 | +``` |
| 183 | +const str = 'google'; |
| 184 | +const str1 = 'gogle'; |
| 185 | +const str2 = 'goooogle'; |
| 186 | +const reg = /go{2,}gle/; |
| 187 | +reg.test(str); // true |
| 188 | +reg.test(str1); // false |
| 189 | +reg.test(str2); // false |
| 190 | +``` |
| 191 | +## Specify the exact number of matches |
| 192 | + |
| 193 | +To specify a certain number of patterns, just have that one number between the curly brackets. |
| 194 | +``` |
| 195 | +const str = 'heel'; |
| 196 | +const str1 = 'heeeeel'; |
| 197 | +const reg = /he{2}l/ |
| 198 | +reg.test(str); // true |
| 199 | +reg.test(str1); // false |
| 200 | +``` |
| 201 | +## Check for all or none |
| 202 | + |
| 203 | +Sometimes the patterns you want to search for may have parts that may or may not exist. |
| 204 | +``` |
| 205 | +const str = 'favourite'; |
| 206 | +const str1 = 'favorite'; |
| 207 | +const str2 = 'faver'; |
| 208 | +const reg = /favou?/; |
| 209 | +reg.test(str); // true |
| 210 | +reg.test(str1); // true |
| 211 | +reg.test(str2); // false |
| 212 | +``` |
| 213 | +## Positive (`?=…`) and negative (`?!…`) lookahead |
| 214 | + |
| 215 | +_Lookaheads_ are patterns that tell JavaScript to look ahead in your string to check for patterns further along. |
| 216 | +``` |
| 217 | +const str = 'abs123';Positive |
| 218 | +const reg = /(?=\w{2,6})/ |
| 219 | +reg.test(str); // trueNegative |
| 220 | +const reg1 = /(?=[a-z]{3})(?!\d{3})/ |
| 221 | +reg1.test(str); // true |
| 222 | +``` |
0 commit comments