|
| 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