变换

使用 magic-regexp 的最佳方式是利用包含的构建时变换。

使用 magic-regexp 的最佳方式是利用包含的构建时变换。

const beforeTransform = createRegExp(exactly('foo/test.js').after('bar/'))
// => gets _compiled_ to
const afterTransform = /(?<=bar\/)foo\/test\.js/

当然,这仅适用于非动态正则表达式。在 createRegExp 块中,您必须包含从 magic-regexp 中使用的所有辅助函数——并且不能依赖任何外部变量。例如,这虽然仍能在最小运行时继续工作,但不会静态编译成 RegExp:

const someString = 'test'
const regExp = createRegExp(exactly(someString))