Skip to content

Commit b78d436

Browse files
committed
extended examples in README
1 parent 9b1e902 commit b78d436

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
# typed-regexp
22
A typescript package that strongly restricts types of regular expressions.
33

4-
## Example
4+
## Examples
5+
6+
As direct dependency: `npm i typed-regexp`
57
```ts
68
import { TypedRegExp } from "typed-regexp";
79

810
const string = "string to match";
911
const regexp = new TypedRegExp<[`{${string}`],{group:"a"|"b"}>("(?<group>[ab])({.*)");
10-
/* can also be this:
12+
const match = string.match(regexp);
13+
if (match) {
14+
const namedGroup = match.groups.group;
15+
// => "a"|"b"
16+
const group = match[1];
17+
// => `{${string}`
18+
}
19+
```
20+
21+
or as dev dependency: `npm i -D typed-regexp`
22+
```ts
23+
import type { TypedRegExp } from "typed-regexp";
24+
25+
const string = "string to match";
1126
const regexp = /(?<group>[ab])({.*)/ as TypedRegExp<[`{${string}`],{group:"a"|"b"}>;
12-
*/
1327
const match = string.match(regexp);
1428
if (match) {
1529
const namedGroup = match.groups.group;

0 commit comments

Comments
 (0)