|
| 1 | +# API |
| 2 | + |
| 3 | +## Builder |
| 4 | + |
| 5 | +### `buildRegex()` function |
| 6 | + |
| 7 | +```ts |
| 8 | +function buildRegex(sequence: RegexSequence): RegExp; |
| 9 | + |
| 10 | +function buildRegex( |
| 11 | + flags: { |
| 12 | + global?: boolean; |
| 13 | + ignoreCase?: boolean; |
| 14 | + multiline?: boolean; |
| 15 | + hasIndices?: boolean; |
| 16 | + sticky?: boolean; |
| 17 | + }, |
| 18 | + sequence: RegexSequence |
| 19 | +): RegExp; |
| 20 | +``` |
| 21 | + |
| 22 | +## Components |
| 23 | + |
| 24 | +### `capture()` component |
| 25 | + |
| 26 | +Captures, also known as capturing groups, are used to extract and store parts of the matched string for later use. |
| 27 | + |
| 28 | +```ts |
| 29 | +function capture( |
| 30 | + sequence: RegexSequence |
| 31 | +): Capture |
| 32 | +``` |
| 33 | + |
| 34 | +### `choiceOf()` component |
| 35 | + |
| 36 | +```ts |
| 37 | +function choiceOf( |
| 38 | + ...alternatives: RegexSequence[] |
| 39 | +): ChoiceOf { |
| 40 | +``` |
| 41 | + |
| 42 | +The `choiceOf` (alternation) construct is used to match one out of several possible sequences. It functions similarly to a logical OR operator in programming. It can match simple string options as well as complex patterns. |
| 43 | + |
| 44 | +Example: `choiceOf("color", "colour")` matches either `color` or `colour` pattern. |
| 45 | + |
| 46 | +## Quantifiers |
| 47 | + |
| 48 | +### `zeroOrMore()` component |
| 49 | + |
| 50 | +```ts |
| 51 | +function zeroOrMore( |
| 52 | + sequence: RegexSequence, |
| 53 | +): ZeroOrMore |
| 54 | +``` |
| 55 | + |
| 56 | +### `oneOrMore()` component |
| 57 | + |
| 58 | +```ts |
| 59 | +function oneOrMore( |
| 60 | + sequence: RegexSequence, |
| 61 | +): OneOrMore |
| 62 | +``` |
| 63 | + |
| 64 | +### `optionally()` component |
| 65 | + |
| 66 | +```ts |
| 67 | +function optionally( |
| 68 | + sequence: RegexSequence, |
| 69 | +): Optionally |
| 70 | +``` |
| 71 | + |
| 72 | +### `repeat()` component |
| 73 | + |
| 74 | +```ts |
| 75 | +function repeat( |
| 76 | + options: { count: number } | { min: number; max?: number }, |
| 77 | + sequence: RegexSequence, |
| 78 | +): Repeat |
| 79 | +``` |
| 80 | + |
| 81 | +## Character classes |
| 82 | + |
| 83 | +Character classes are a set of characters that match any one of the characters in the set. |
| 84 | + |
| 85 | +### Common character classess |
| 86 | + |
| 87 | +```ts |
| 88 | +const any: CharacterClass; |
| 89 | +const word: CharacterClass; |
| 90 | +const digit: CharacterClass; |
| 91 | +const whitespace: CharacterClass; |
| 92 | +``` |
| 93 | + |
| 94 | +* `any` matches any character except newline characters. |
| 95 | +* `word` matches any word character (alphanumeric & underscore). |
| 96 | +* `digit` matches any digit. |
| 97 | +* `whitespace` matches any whitespace character (spaces, tabs, line breaks). |
| 98 | + |
| 99 | +### `anyOf()` component |
| 100 | + |
| 101 | +```ts |
| 102 | +function anyOf( |
| 103 | + characters: string, |
| 104 | +): CharacterClass |
| 105 | +``` |
| 106 | + |
| 107 | +The `anyOf` class matches any character present in the `character` string. |
| 108 | + |
| 109 | +Example: `anyOf('aeiou')` will match either `a`, `e`, `i` `o` or `u` characters. |
| 110 | + |
| 111 | +### `characterRange()` component |
| 112 | + |
| 113 | +```ts |
| 114 | +function characterRange( |
| 115 | + start: string, |
| 116 | + end: string, |
| 117 | +): CharacterClass |
| 118 | +``` |
| 119 | + |
| 120 | +The `characterRange` class matches any character present in the range from `start` to `end` (inclusive). |
| 121 | + |
| 122 | +Examples: |
| 123 | +* `characterRange('a', 'z')` will match all lowercase characters from `a` to `z`. |
| 124 | +* `characterRange('A', 'Z')` will match all uppercase characters from `a` to `z`. |
| 125 | +* `characterRange('0', '9')` will match all digit characters from `0` to `9`. |
| 126 | + |
| 127 | +### `characterClass()` component |
| 128 | + |
| 129 | +```ts |
| 130 | +function characterClass( |
| 131 | + ...elements: CharacterClass[], |
| 132 | +): CharacterClass |
| 133 | +``` |
| 134 | + |
| 135 | +The `characterClass` component creates a new character class that includes all passed character classes. |
| 136 | + |
| 137 | +Example: |
| 138 | +* `characterClass(characterRange('a', 'f'), digit)` will match all lowercase hex digits (`0` to `9` and `a` to `f`). |
| 139 | +* `characterClass(characterRange('a', 'z'), digit, anyOf("._-"))` will match any digit, lowercase latin lettet from `a` to `z`, and either of `.`, `_`, and `-` characters. |
| 140 | + |
| 141 | +### `inverted()` component |
| 142 | + |
| 143 | +```ts |
| 144 | +function inverted( |
| 145 | + element: CharacterClass, |
| 146 | +): CharacterClass |
| 147 | +``` |
| 148 | + |
| 149 | +The `inverted` component creates a new character class that matches any character that is not present in the passed character class. |
| 150 | + |
| 151 | +Examples: |
| 152 | +* `inverted(digit)` matches any character that is not a digit |
| 153 | +* `inverted(anyOf('aeiou'))` matches any character that is not a lowercase vowel. |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | +## Anchors |
| 158 | + |
| 159 | +Anchors are special characters or sequences that specify positions in the input string, rather than matching specific characters. |
| 160 | + |
| 161 | +### Line start and end |
| 162 | + |
| 163 | +```ts |
| 164 | +const startOfString: Anchor; // Regex: ^ |
| 165 | +const endOfString: Anchor; // Regex: $ |
| 166 | +``` |
| 167 | + |
| 168 | +The `startOfString` (regex: `^`) matches the start of a string (or line, if multiline mode is enabled). |
| 169 | + |
| 170 | +The `endOfString` (regex: `$`) matches the end of a string (or line, if multiline mode is enabled). |
0 commit comments