Skip to content

Commit 5e6d661

Browse files
author
Carlos
committed
Add new cheat sheet for Regular Expressions
1 parent 79adc18 commit 5e6d661

File tree

2 files changed

+280
-0
lines changed

2 files changed

+280
-0
lines changed
Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
---
2+
title: Javascript Regular Expressions - Javascript Cheatsheet
3+
description: Often abbreviated as regex or regexp, are sequences of characters that form a search pattern.
4+
---
5+
6+
<base-title :title="frontmatter.title" :description="frontmatter.description">
7+
Javascript Regular Expressions
8+
</base-title>
9+
10+
<base-disclaimer>
11+
<base-disclaimer-title>
12+
From the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions">MDN web docs</a>
13+
</base-disclaimer-title>
14+
<base-disclaimer-content>
15+
Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. These patterns are used with the exec() and test() methods of RegExp, and with the match(), matchAll(), replace(), replaceAll(), search(), and split() methods of String. This chapter describes JavaScript regular expressions.
16+
</base-disclaimer-content>
17+
</base-disclaimer>
18+
19+
## Regular Expression Symbols
20+
21+
| Symbol | Description |
22+
| -------- | ---------------------------------------------------------------------------- |
23+
| `.` | Matches any character except newline |
24+
| `\d` | Matches any digit (0-9) |
25+
| `\D` | Matches any non-digit character |
26+
| `\s` | Matches any whitespace character |
27+
| `\S` | Matches any non-whitespace character |
28+
| `\w` | Matches any alphanumeric character (a-z, A-Z, 0-9) and underscore (_) |
29+
| `\W` | Matches any non-alphanumeric character |
30+
| `[abc]` | Matches any character inside the brackets (a, b, or c) |
31+
| `[^abc]` | Matches any character not inside the brackets |
32+
| `^` | Matches the start of the string |
33+
| `$` | Matches the end of the string |
34+
| `*` | Matches zero or more occurrences of the preceding element |
35+
| `+` | Matches one or more occurrences of the preceding element |
36+
| `?` | Matches zero or one occurrence of the preceding element |
37+
| `{n}` | Matches exactly n occurrences of the preceding element |
38+
| `{n,}` | Matches n or more occurrences of the preceding element |
39+
| `{n,m}` | Matches at least n and at most m occurrences of the preceding element |
40+
| `\|` | Acts as a boolean OR. Matches the pattern before or the pattern after the \| |
41+
| `( )` | Defines a group |
42+
| `(?: )` | Defines a non-capturing group |
43+
| `(?= )` | Positive lookahead |
44+
| `(?! )` | Negative lookahead |
45+
46+
<base-disclaimer>
47+
<base-disclaimer-title>
48+
Complex Patterns
49+
</base-disclaimer-title>
50+
<base-disclaimer-content>
51+
Please note that following examples are simple examples and regular expressions can be combined to create more complex patterns.
52+
</base-disclaimer-content>
53+
</base-disclaimer>
54+
55+
## Dot (.)
56+
57+
Matches any character except newline.
58+
59+
```javascript
60+
let regex = /a.c/;
61+
let str = 'abc';
62+
console.log(regex.test(str)); // true
63+
```
64+
65+
## Digit (\d)
66+
67+
Matches any digit (0-9).
68+
69+
```javascript
70+
let regex = /\d/;
71+
let str = 'abc123';
72+
console.log(regex.test(str)); // true
73+
```
74+
75+
## Non-Digit (\D)
76+
77+
Matches any non-digit character.
78+
79+
```javascript
80+
let regex = /\D/;
81+
let str = 'abc123';
82+
console.log(regex.test(str)); // true
83+
```
84+
85+
## Whitespace (\s)
86+
87+
Matches any whitespace character.
88+
89+
```javascript
90+
let regex = /\s/;
91+
let str = 'abc def';
92+
console.log(regex.test(str)); // true
93+
```
94+
95+
## Non-Whitespace (\S)
96+
97+
Matches any non-whitespace character.
98+
99+
```javascript
100+
let regex = /\S/;
101+
let str = ' abc';
102+
console.log(regex.test(str)); // true
103+
```
104+
105+
## Word Character (\w)
106+
107+
Matches any alphanumeric character (a-z, A-Z, 0-9) and underscore (_).
108+
109+
```javascript
110+
let regex = /\w/;
111+
let str = 'abc';
112+
console.log(regex.test(str)); // true
113+
```
114+
115+
## Non-Word Character (\W)
116+
117+
Matches any non-alphanumeric character.
118+
119+
```javascript
120+
let regex = /\W/;
121+
let str = 'abc-def';
122+
console.log(regex.test(str)); // true
123+
```
124+
125+
## Character Set ([abc])
126+
127+
Matches any character inside the brackets (a, b, or c).
128+
129+
```javascript
130+
let regex = /[abc]/;
131+
let str = 'defabc';
132+
console.log(regex.test(str)); // true
133+
```
134+
135+
## Negated Character Set ([^abc])
136+
137+
Matches any character not inside the brackets.
138+
139+
```javascript
140+
let regex = /[^abc]/;
141+
let str = 'defabc';
142+
console.log(regex.test(str)); // true
143+
```
144+
145+
## Start Anchor (^)
146+
147+
Matches the start of the string.
148+
149+
```javascript
150+
let regex = /^abc/;
151+
let str = 'abcdef';
152+
console.log(regex.test(str)); // true
153+
```
154+
155+
## End Anchor ($)
156+
157+
Matches the end of the string.
158+
159+
```javascript
160+
let regex = /def$/;
161+
let str = 'abcdef';
162+
console.log(regex.test(str)); // true
163+
```
164+
165+
## Zero or More (*):** Matches zero or more occurrences of the preceding element.
166+
167+
```javascript
168+
let regex = /a*/;
169+
let str = 'aaaabc';
170+
console.log(regex.test(str)); // true
171+
```
172+
173+
## One or More (+)
174+
175+
Matches one or more occurrences of the preceding element.
176+
177+
```javascript
178+
let regex = /a+/;
179+
let str = 'aaaabc';
180+
console.log(regex.test(str)); // true
181+
```
182+
183+
## Zero or One (?)
184+
185+
Matches zero or one occurrence of the preceding element.
186+
187+
```javascript
188+
let regex = /a?/;
189+
let str = 'abc';
190+
console.log(regex.test(str)); // true
191+
```
192+
193+
## Exactly N ({n})
194+
195+
Matches exactly n occurrences of the preceding element.
196+
197+
```javascript
198+
let regex = /a{2}/;
199+
let str = 'aaaabc';
200+
console.log(regex.test(str)); // true
201+
```
202+
203+
## N or More ({n,})
204+
205+
Matches n or more occurrences of the preceding element.
206+
207+
```javascript
208+
let regex = /a{2,}/;
209+
let str = 'aaaabc';
210+
console.log(regex.test(str)); // true
211+
```
212+
213+
## Between N and M ({nm})
214+
215+
Matches at least n and at most m occurrences of the preceding element.
216+
217+
```javascript
218+
let regex = /a{2,3}/;
219+
let str = 'aaaabc';
220+
console.log(regex.test(str)); // true
221+
```
222+
223+
## OR (|)
224+
225+
Acts as a boolean OR. Matches the pattern before or the pattern after the `|`.
226+
227+
```javascript
228+
let regex = /abc|def/;
229+
let str1 = 'abc';
230+
let str2 = 'def';
231+
console.log(regex.test(str1)); // true
232+
console.log(regex.test(str2)); // true
233+
```
234+
235+
## Grouping (()
236+
237+
Defines a group.
238+
239+
```javascript
240+
let regex = /(abc)/;
241+
let str = 'abcdef';
242+
console.log(regex.test(str)); // true
243+
```
244+
245+
## Non-Capturing Group ((?: ))
246+
247+
Defines a non-capturing group.
248+
249+
```javascript
250+
let regex = /(?:abc)/;
251+
let str = 'abcdef';
252+
console.log(regex.test(str)); // true
253+
```
254+
255+
Sure, here is the continuation from item 21:
256+
257+
## Positive Lookahead ((?= ))
258+
259+
Positive lookahead.
260+
261+
```javascript
262+
let regex = /abc(?=def)/;
263+
let str = 'abcdef';
264+
console.log(regex.test(str)); // true
265+
```
266+
267+
## Negative Lookahead ((?! ))
268+
269+
Negative lookahead.
270+
271+
```javascript
272+
let regex = /abc(?!def)/;
273+
let str = 'abcghi';
274+
console.log(regex.test(str)); // true
275+
```

src/store/navigation.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ export const useNavigationStore = defineStore('navigation', {
7070
path: '/cheatsheet/string-formatting',
7171
updated: false,
7272
},
73+
{
74+
name: 'Regular Expressions',
75+
path: '/cheatsheet/regular-expressions',
76+
updated: true,
77+
},
7378
{
7479
name: 'Files and Directories',
7580
path: '/cheatsheet/directory-files',

0 commit comments

Comments
 (0)