Skip to content

Commit f125552

Browse files
committed
add some additional special chars and use string.raw
1 parent b71a91d commit f125552

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

packages/open-next/src/utils/regex.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export function getCrossPlatformPathRegex(
1414
opts: { escape: boolean } = { escape: true },
1515
) {
1616
const newExpr = (
17-
opts.escape ? regex.replace(/([[\]().*+?^$|])/g, "\\$1") : regex
18-
).replaceAll("/", "(?:\\/|\\\\)");
17+
opts.escape ? regex.replace(/([[\]().*+?^$|{}\\])/g, "\\$1") : regex
18+
).replaceAll("/", String.raw`(?:\/|\\)`);
1919

2020
return new RegExp(newExpr, "g");
2121
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
22

3-
const specialChars = "^([123]+|[123]*)?$";
3+
const specialChars = "^([123]+|[123]{1,3})*\\?$";
44

55
describe("getCrossPlatformPathRegex", () => {
66
it("should return a regex without escaping characters", () => {
@@ -11,12 +11,14 @@ describe("getCrossPlatformPathRegex", () => {
1111
it("should always create cross-platform separators", () => {
1212
[true, false].forEach((v) => {
1313
const regexp = getCrossPlatformPathRegex("test/path", { escape: v });
14-
expect(regexp.source).toEqual("test(?:\\/|\\\\)path");
14+
expect(regexp.source).toEqual(String.raw`test(?:\/|\\)path`);
1515
});
1616
});
1717

1818
it("should return a regex with escaped characters", () => {
1919
const regexp = getCrossPlatformPathRegex(specialChars, { escape: true });
20-
expect(regexp.source).toEqual("\\^\\(\\[123\\]\\+\\|\\[123\\]\\*\\)\\?\\$");
20+
expect(regexp.source).toEqual(
21+
String.raw`\^\(\[123\]\+\|\[123\]\{1,3\}\)\*\\\?\$`,
22+
);
2123
});
2224
});

0 commit comments

Comments
 (0)