Skip to content

Commit ac35b74

Browse files
feat: dotAll & sticky options (#84)
1 parent 49e8aa4 commit ac35b74

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

src/__tests__/builder.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@ test('`regexBuilder` flags', () => {
1616
expect(buildRegExp('a', { hasIndices: true }).flags).toBe('d');
1717
expect(buildRegExp('a', { hasIndices: false }).flags).toBe('');
1818

19+
expect(buildRegExp('a', { dotAll: true }).flags).toBe('s');
20+
expect(buildRegExp('a', { dotAll: false }).flags).toBe('');
21+
22+
expect(buildRegExp('a', { sticky: true }).flags).toBe('y');
23+
expect(buildRegExp('a', { sticky: false }).flags).toBe('');
24+
1925
expect(
2026
buildRegExp('a', {
2127
global: true, //
2228
ignoreCase: true,
2329
multiline: false,
30+
dotAll: true,
31+
sticky: true,
2432
}).flags,
25-
).toBe('gi');
33+
).toBe('gisy');
2634
});

src/builders.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ function encodeFlags(flags: RegexFlags): string {
3131
if (flags.ignoreCase) result += 'i';
3232
if (flags.multiline) result += 'm';
3333
if (flags.hasIndices) result += 'd';
34+
if (flags.dotAll) result += 's';
35+
if (flags.sticky) result += 'y';
3436

3537
return result;
3638
}

src/types.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,44 @@ export interface RegexConstruct {
2222
encode(): EncodeResult;
2323
}
2424

25+
/**
26+
* Flags to be passed to RegExp constructor.
27+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp#flags
28+
*/
2529
export interface RegexFlags {
26-
/** Find all matches in a string, instead of just the first one. */
30+
/**
31+
* Find all matches in a string, instead of just the first one.
32+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/global
33+
*/
2734
global?: boolean;
2835

29-
/** Perform case-insensitive matching. */
36+
/**
37+
* Perform case-insensitive matching.
38+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/ignoreCase
39+
*/
3040
ignoreCase?: boolean;
3141

32-
/** Treat the start and end of each line in a string as the beginning and end of the string. */
42+
/**
43+
* Treat the start and end of each line in a string as the beginning and end of the string.
44+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/multiline
45+
*/
3346
multiline?: boolean;
3447

35-
/** Penerate the start and end indices of each captured group in a match. */
48+
/**
49+
* Generate the start and end indices of each captured group in a match.
50+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/hasIndices
51+
*/
3652
hasIndices?: boolean;
53+
54+
/**
55+
* MDN: _Allows . to match newlines._
56+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/dotAll
57+
*/
58+
dotAll?: boolean;
59+
60+
/**
61+
* MDN: _Matches only from the index indicated by the lastIndex property of this regular expression in the target string. Does not attempt to match from any later indexes._
62+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky
63+
*/
64+
sticky?: boolean;
3765
}

website/docs/api/builder.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ function buildRegExp(
1313
ignoreCase?: boolean;
1414
multiline?: boolean;
1515
hasIndices?: boolean;
16+
dotAll?: boolean;
17+
sticky?: boolean;
1618
},
1719
): RegExp;
1820
```
@@ -25,3 +27,5 @@ It optionally accepts a list of regex flags:
2527
- `ignoreCase` - perform case-insensitive matching.
2628
- `multiline` - treat the start and end of each line in a string as the beginning and end of the string.
2729
- `hasIndices` - provide each captured group's start and end indices in a match.
30+
- `dotAll` - allows `.` to match newlines.
31+
- `sticky` - matches only from the index indicated by the `lastIndex` property, does not attempt to match from any later indexes.

0 commit comments

Comments
 (0)