diff --git a/src/core/internal.ts b/src/core/internal.ts index c72909f2..f85c62ed 100644 --- a/src/core/internal.ts +++ b/src/core/internal.ts @@ -37,6 +37,15 @@ export interface Input< before: ( input: I ) => Input<`${V}(?=${GetValue})`, G, [...C, ...GetCapturedGroupsArr]> + + between: ( + input1: I1, + input2: I2 + ) => Input< + `(?<=${GetValue})${V}(?=${GetValue})`, + G, + [...GetCapturedGroupsArr, ...C, ...GetCapturedGroupsArr] + > /** these is a negative lookbehind. Make sure to check [browser support](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#browser_compatibility) as not all browsers support lookbehinds (notably Safari) */ notAfter: ( input: I @@ -110,6 +119,7 @@ export const createInput = < or: input => createInput(`(?:${s}|${exactly(input)})`), after: input => createInput(`(?<=${exactly(input)})${s}`), before: input => createInput(`${s}(?=${exactly(input)})`), + between: (input1, input2) => createInput(`(?<=${exactly(input1)})${s}(?=${exactly(input2)})`), notAfter: input => createInput(`(? createInput(`${s}(?!${exactly(input)})`), times: Object.assign((number: number) => createInput(`${wrap(s)}{${number}}`) as any, { diff --git a/test/index.test.ts b/test/index.test.ts index cba96c59..20283d25 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -68,6 +68,14 @@ describe('inputs', () => { expect('fooafoo'.match(regExp)?.[0]).toMatchInlineSnapshot('"a"') expect(regExp.test('foo')).toBeFalsy() }) + + it('between', () => { + const regExp = createRegExp(char.between('foo', 'bar')) + expect(regExp).toMatchInlineSnapshot('/\\(\\?<=foo\\)\\.\\(\\?=bar\\)/') + expect('fooabar'.match(regExp)?.[0]).toMatchInlineSnapshot('"a"') + expect(regExp.test('foo')).toBeFalsy() + expect(regExp.test('bar')).toBeFalsy() + }) it('notBefore', () => { const regExp = createRegExp(exactly('bar').notBefore('foo')) expect(regExp).toMatchInlineSnapshot('/bar\\(\\?!foo\\)/')