@@ -8,13 +8,22 @@ import { onigurumaToRegexp } from 'oniguruma-to-js'
8
8
export interface JavaScriptRegexEngineOptions {
9
9
/**
10
10
* Whether to allow invalid regex patterns.
11
+ *
12
+ * @default false
11
13
*/
12
14
forgiving ?: boolean
13
15
16
+ /**
17
+ * Use JavaScript to simulate some unsupported regex features.
18
+ *
19
+ * @default true
20
+ */
21
+ simulation ?: boolean
22
+
14
23
/**
15
24
* Cache for regex patterns.
16
25
*/
17
- cache ?: Map < string , RegExp | Error >
26
+ cache ?: Map < string , RegExp | Error > | null
18
27
19
28
/**
20
29
* Custom pattern to RegExp constructor.
@@ -45,13 +54,18 @@ export class JavaScriptScanner implements PatternScanner {
45
54
46
55
constructor (
47
56
public patterns : string [ ] ,
48
- public cache : Map < string , RegExp | Error > ,
49
- public forgiving : boolean ,
50
- public regexConstructor : ( pattern : string ) => RegExp = defaultJavaScriptRegexConstructor ,
57
+ public options : JavaScriptRegexEngineOptions = { } ,
51
58
) {
59
+ const {
60
+ forgiving = false ,
61
+ cache,
62
+ simulation = true ,
63
+ regexConstructor = defaultJavaScriptRegexConstructor ,
64
+ } = options
65
+
52
66
this . contiguousAnchorSimulation = Array . from ( { length : patterns . length } , ( ) => false )
53
67
this . regexps = patterns . map ( ( p , idx ) => {
54
- if ( p . startsWith ( '(^|\\G)' ) || p . startsWith ( '(\\G|^)' ) )
68
+ if ( simulation && ( p . startsWith ( '(^|\\G)' ) || p . startsWith ( '(\\G|^)' ) ) )
55
69
this . contiguousAnchorSimulation [ idx ] = true
56
70
const cached = cache ?. get ( p )
57
71
if ( cached ) {
@@ -129,7 +143,7 @@ export class JavaScriptScanner implements PatternScanner {
129
143
pending . push ( [ i , match , offset ] )
130
144
}
131
145
catch ( e ) {
132
- if ( this . forgiving )
146
+ if ( this . options . forgiving )
133
147
continue
134
148
throw e
135
149
}
@@ -159,14 +173,14 @@ export class JavaScriptScanner implements PatternScanner {
159
173
* @experimental
160
174
*/
161
175
export function createJavaScriptRegexEngine ( options : JavaScriptRegexEngineOptions = { } ) : RegexEngine {
162
- const {
163
- forgiving = false ,
164
- cache = new Map ( ) ,
165
- } = options
176
+ const _options = {
177
+ cache : new Map ( ) ,
178
+ ... options ,
179
+ }
166
180
167
181
return {
168
182
createScanner ( patterns : string [ ] ) {
169
- return new JavaScriptScanner ( patterns , cache , forgiving , options . regexConstructor )
183
+ return new JavaScriptScanner ( patterns , _options )
170
184
} ,
171
185
createString ( s : string ) {
172
186
return {
0 commit comments