Skip to content

Commit cac350b

Browse files
committed
Added regular expressions
1 parent c3cf7bf commit cac350b

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ This repository contains Vanilla JavaScript topics and their notes to learn from
9292
- [try and catch](./docs/error-handling.md#-try-and-catch)
9393
- [Custom error objects](./docs/error-handling.md#-custom-error-objects)
9494
- [Necessity of Error Handling](./docs/error-handling.md#-necessity-of-error-handling)
95-
2. Regular Expressions:
96-
- Creating regular expressions
97-
- Common regular expression patterns
98-
- Matching patterns in strings
95+
2. [Regular Expressions:](./docs/regular-expressions.md)
96+
- [Creating regular expressions](./docs/regular-expressions.md#-creating-regular-expressions)
97+
- [Common regular expression patterns](./docs/regular-expressions.md#-common-regular-expression-patterns)
98+
- [Matching patterns in strings](./docs/regular-expressions.md#-matching-patterns-in-strings)
9999
3. JSON (JavaScript Object Notation):
100100
- Parsing and manipulating JSON data
101101
4. JavaScript Engine:

docs/regular-expressions.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## ⚑ Regular Expressions:
2+
A regular expression, often abbreviated as `regex`. It is a sequence of characters that defines a search pattern. It's used to match, find, replace, or manipulate text based on given patterns.
3+
4+
### ☴ Overview:
5+
1. [Creating regular expressions](#-creating-regular-expressions)
6+
2. [Common regular expression patterns](#-common-regular-expression-patterns)
7+
3. [Matching patterns in strings](#-matching-patterns-in-strings)
8+
9+
### ✦ Creating regular expressions:
10+
Regular expressions can be created in two ways:
11+
- Literal notation:
12+
```javascript
13+
let pattern = /pattern/;
14+
```
15+
- Constructor:
16+
```javascript
17+
let pattern = new RegExp("pattern");
18+
```
19+
20+
**Flags:**
21+
Regular expressions can have flags that modify their behavior:
22+
- `i`: Case-insensitive matching.
23+
- `g`: Global matching, finding all matches instead of just the first.
24+
- `m`: Multiline matching, allowing `^` and `$` to match the beginning and end of lines, respectively.
25+
26+
### ✦ Common regular expression patterns:
27+
- `.`: Matches any character except a newline.
28+
- `^`: Matches the beginning of a string or line.
29+
- `$`: Matches the end of a string or line.
30+
- `*`: Matches zero or more occurrences of the preceding character or group.
31+
- `+`: Matches one or more occurrences of the preceding character or group.
32+
- `?`: Matches zero or one occurrence of the preceding character or group.  
33+
- `{n}`: Matches exactly n occurrences of the preceding character or group.
34+
- `{n,}`: Matches at least n occurrences of the preceding character or group.  
35+
- `{n,m}`: Matches between n and m occurrences of the preceding character or group.  
36+
- `|`: Matches either the expression before or after it.
37+
- `()`: Groups a part of the regular expression.
38+
- `[ ]`: Matches any character within the brackets.
39+
- `[^ ]`: Matches any character not within the brackets
40+
41+
### ✦ Matching patterns in strings:
42+
```javascript
43+
let text = "The quick brown fox jumps over the lazy dog.";
44+
let pattern = /fox/;
45+
46+
let match = text.match(pattern);
47+
console.log(match); // Output: ["fox"]
48+
49+
let index = text.search(pattern);
50+
console.log(index); // Output: 16
51+
52+
let replacedText = text.replace(pattern, "cat");
53+
console.log(replacedText); // Output: "The quick brown cat jumps over the lazy dog."
54+
```
55+
56+
**Other Methods:**
57+
There are several methods to work with regular expressions:
58+
59+
- `match()`: Finds all matches of a regular expression in a string and returns an array.
60+
- `search()`: Finds the index of the first match of a regular expression in a string.
61+
- `replace()`: Replaces all matches of a regular expression in a string with a new string.
62+
- `test()`: Checks if a string matches a regular expression and returns a boolean.
63+
- `split()`: Splits a string into an array using a regular expression as a separator.
64+
65+
---
66+
[⇪ To Top](#-regular-expressions)
67+
68+
[❮ Previous Topic](./error-handling.md)   [Next Topic ❯](./json.md)
69+
70+
[⌂ Goto Home Page](../README.md)

0 commit comments

Comments
 (0)