From 3b292f01249d64027fd3282aafc4536d5d8c0c21 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Tue, 7 Apr 2026 11:04:40 +0200 Subject: [PATCH 1/3] fix(helpers): ignore wrapper characters in fromRegExp --- src/modules/helpers/index.ts | 2 +- test/modules/helpers.spec.ts | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index e88ed6ec792..5f88e39acc7 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -371,7 +371,7 @@ export class SimpleHelpersModule extends SimpleModuleBase { if (pattern instanceof RegExp) { isCaseInsensitive = pattern.flags.includes('i'); pattern = pattern.toString(); - pattern = /\/(.+?)\//.exec(pattern)?.[1] ?? ''; // Remove frontslash from front and back of RegExp + pattern = /\/\^?(.+?)\$?\//.exec(pattern)?.[1] ?? ''; // Remove slash and anchors from front and back of RegExp } let min: number; diff --git a/test/modules/helpers.spec.ts b/test/modules/helpers.spec.ts index a521b49873a..28dc0621aee 100644 --- a/test/modules/helpers.spec.ts +++ b/test/modules/helpers.spec.ts @@ -689,6 +689,11 @@ describe('helpers', () => { expect(actual).toHaveLength(9); expect(actual).toMatch(/^[A-D0-9]{4}-[A-D0-9]{4}$/i); }); + + it('hides regex wrapper characters', () => { + const actual = faker.helpers.fromRegExp(/^foo$/i); + expect(actual).toBe('foo'); + }); }); describe('shuffle()', () => { From c3653f0ae8ed704fb862bcef5c29161a4570dd67 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Sun, 12 Apr 2026 18:52:35 +0200 Subject: [PATCH 2/3] chore: simplify RegExp source extraction --- src/modules/helpers/index.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index 5f88e39acc7..f3869b1df09 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -370,8 +370,7 @@ export class SimpleHelpersModule extends SimpleModuleBase { if (pattern instanceof RegExp) { isCaseInsensitive = pattern.flags.includes('i'); - pattern = pattern.toString(); - pattern = /\/\^?(.+?)\$?\//.exec(pattern)?.[1] ?? ''; // Remove slash and anchors from front and back of RegExp + pattern = pattern.source.replace(/^\^/, '').replace(/\$$/, ''); } let min: number; From 5aeddd4b72a0f295fc33b65c2d9d0de111f062ef Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Sun, 19 Apr 2026 21:43:13 +0200 Subject: [PATCH 3/3] chore: fix repeated wrappers --- src/modules/helpers/index.ts | 2 +- test/modules/helpers.spec.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index f3869b1df09..921fc1898ae 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -370,7 +370,7 @@ export class SimpleHelpersModule extends SimpleModuleBase { if (pattern instanceof RegExp) { isCaseInsensitive = pattern.flags.includes('i'); - pattern = pattern.source.replace(/^\^/, '').replace(/\$$/, ''); + pattern = pattern.source.replace(/^\^+/, '').replace(/\$+$/, ''); } let min: number; diff --git a/test/modules/helpers.spec.ts b/test/modules/helpers.spec.ts index 28dc0621aee..aec8dd228ef 100644 --- a/test/modules/helpers.spec.ts +++ b/test/modules/helpers.spec.ts @@ -691,7 +691,7 @@ describe('helpers', () => { }); it('hides regex wrapper characters', () => { - const actual = faker.helpers.fromRegExp(/^foo$/i); + const actual = faker.helpers.fromRegExp(/^^foo$$/i); expect(actual).toBe('foo'); }); });