diff --git a/test/common/assertSnapshot.js b/test/common/assertSnapshot.js
index 0e350cd1dac6fa..af4345f5111f24 100644
--- a/test/common/assertSnapshot.js
+++ b/test/common/assertSnapshot.js
@@ -109,7 +109,7 @@ function replaceTestDuration(str) {
const root = path.resolve(__dirname, '..', '..');
const color = '(\\[\\d+m)';
-const stackTraceBasePath = new RegExp(`${color}\\(${root.replaceAll(/[\\^$*+?.()|[\]{}]/g, '\\$&')}/?${color}(.*)${color}\\)`, 'g');
+const stackTraceBasePath = new RegExp(`${color}\\(${RegExp.escape(root)}/?${color}(.*)${color}\\)`, 'g');
function replaceSpecDuration(str) {
return str
diff --git a/test/parallel/test-crypto-argon2.js b/test/parallel/test-crypto-argon2.js
index 268b722cf81bb2..c8015d00458ac1 100644
--- a/test/parallel/test-crypto-argon2.js
+++ b/test/parallel/test-crypto-argon2.js
@@ -112,7 +112,7 @@ for (const [algorithm, overrides, expected] of good) {
for (const [algorithm, overrides, param] of bad) {
const expected = {
code: 'ERR_OUT_OF_RANGE',
- message: new RegExp(`The value of "${param}" is out of range`),
+ message: new RegExp(`The value of "${RegExp.escape(param)}" is out of range`),
};
const parameters = { ...defaults, ...overrides };
assert.throws(() => crypto.argon2(algorithm, parameters, () => {}), expected);
@@ -122,7 +122,7 @@ for (const [algorithm, overrides, param] of bad) {
for (const key of Object.keys(defaults)) {
const expected = {
code: 'ERR_INVALID_ARG_TYPE',
- message: new RegExp(`"parameters\\.${key}"`),
+ message: new RegExp(`"parameters\\.${RegExp.escape(key)}"`),
};
const parameters = { ...defaults };
delete parameters[key];
diff --git a/test/parallel/test-crypto-x509.js b/test/parallel/test-crypto-x509.js
index e1a7701a03b14d..a122ee9e300f30 100644
--- a/test/parallel/test-crypto-x509.js
+++ b/test/parallel/test-crypto-x509.js
@@ -296,7 +296,7 @@ oans248kpal88CGqsN2so/wZKxVnpiXlPHMdiNL7hRSUqlHkUi07FrP2Htg8kjI=
'OCSP - URI': ['http://ocsp.nodejs.org/'],
'CA Issuers - URI': ['http://ca.nodejs.org/ca.cert']
}),
- modulusPattern: new RegExp(`^${modulusOSSL}$`, 'i'),
+ modulusPattern: new RegExp(`^${RegExp.escape(modulusOSSL)}$`, 'i'),
bits: 2048,
exponent: '0x10001',
valid_from: 'Sep 3 21:40:37 2022 GMT',
diff --git a/test/parallel/test-module-loading-globalpaths.js b/test/parallel/test-module-loading-globalpaths.js
index a7b846019957d6..fa1196a12bb3fc 100644
--- a/test/parallel/test-module-loading-globalpaths.js
+++ b/test/parallel/test-module-loading-globalpaths.js
@@ -54,7 +54,7 @@ if (process.argv[2] === 'child') {
child_process.execFileSync(testExecPath, [ __filename, 'child' ],
{ encoding: 'utf8', env: env });
},
- new RegExp(`Cannot find module '${pkgName}'`));
+ new RegExp(`Cannot find module '${RegExp.escape(pkgName)}'`));
// Test module in $HOME/.node_modules.
const modHomeDir = path.join(testFixturesDir, 'home-pkg-in-node_modules');
diff --git a/test/parallel/test-permission-warning-flags.js b/test/parallel/test-permission-warning-flags.js
index e203f4bc78b210..450597942c6933 100644
--- a/test/parallel/test-permission-warning-flags.js
+++ b/test/parallel/test-permission-warning-flags.js
@@ -21,6 +21,6 @@ for (const flag of warnFlags) {
]
);
- assert.match(stderr.toString(), new RegExp(`SecurityWarning: The flag ${flag} must be used with extreme caution`));
+ assert.match(stderr.toString(), new RegExp(`SecurityWarning: The flag ${RegExp.escape(flag)} must be used with extreme caution`));
assert.strictEqual(status, 0);
}
diff --git a/test/parallel/test-process-env-allowed-flags-are-documented.js b/test/parallel/test-process-env-allowed-flags-are-documented.js
index 68df0baebad19d..338a1506700031 100644
--- a/test/parallel/test-process-env-allowed-flags-are-documented.js
+++ b/test/parallel/test-process-env-allowed-flags-are-documented.js
@@ -12,7 +12,7 @@ const cliMd = path.join(rootDir, 'doc', 'api', 'cli.md');
const cliText = fs.readFileSync(cliMd, { encoding: 'utf8' });
const parseSection = (text, startMarker, endMarker) => {
- const regExp = new RegExp(`${startMarker}\r?\n([^]*)\r?\n${endMarker}`);
+ const regExp = new RegExp(`${RegExp.escape(startMarker)}\r?\n([^]*)\r?\n${RegExp.escape(endMarker)}`);
const match = text.match(regExp);
assert(match,
`Unable to locate text between '${startMarker}' and '${endMarker}'.`);
diff --git a/test/parallel/test-quic-internal-endpoint-options.mjs b/test/parallel/test-quic-internal-endpoint-options.mjs
index 951ad3cf032f99..e6f87ba413f612 100644
--- a/test/parallel/test-quic-internal-endpoint-options.mjs
+++ b/test/parallel/test-quic-internal-endpoint-options.mjs
@@ -148,7 +148,7 @@ for (const { key, valid, invalid } of cases) {
const options = {};
options[key] = value;
throws(() => new QuicEndpoint(options), {
- message: new RegExp(`${key}`),
+ message: new RegExp(`${RegExp.escape(key)}`),
}, value);
}
}
diff --git a/test/parallel/test-release-changelog.js b/test/parallel/test-release-changelog.js
index 99889fa1724014..c78efeeff0cf68 100644
--- a/test/parallel/test-release-changelog.js
+++ b/test/parallel/test-release-changelog.js
@@ -8,7 +8,7 @@ const fs = require('fs');
const path = require('path');
const getDefine = (text, name) => {
- const regexp = new RegExp(`#define\\s+${name}\\s+(.*)`);
+ const regexp = new RegExp(`#define\\s+${RegExp.escape(name)}\\s+(.*)`);
const match = regexp.exec(text);
assert.notStrictEqual(match, null);
return match[1];
@@ -27,7 +27,7 @@ if (!release) {
const major = getDefine(versionText, 'NODE_MAJOR_VERSION');
const minor = getDefine(versionText, 'NODE_MINOR_VERSION');
const patch = getDefine(versionText, 'NODE_PATCH_VERSION');
-const versionForRegex = `${major}\\.${minor}\\.${patch}`;
+const versionForRegex = RegExp.escape(`${major}.${minor}.${patch}`);
const lts = getDefine(versionText, 'NODE_VERSION_IS_LTS') !== '0';
const codename = getDefine(versionText, 'NODE_VERSION_LTS_CODENAME').slice(1, -1);
@@ -45,7 +45,7 @@ const changelogPath = `doc/changelogs/CHANGELOG_V${major}.md`;
// Check table header
let tableHeader;
if (lts) {
- tableHeader = new RegExp(`
LTS '${codename}' | `);
+ tableHeader = new RegExp(`LTS '${RegExp.escape(codename)}' | `);
} else {
tableHeader = /Current<\/th>/;
}
@@ -57,7 +57,7 @@ const changelogPath = `doc/changelogs/CHANGELOG_V${major}.md`;
// Check title for changelog entry.
let title;
if (lts) {
- title = new RegExp(`## \\d{4}-\\d{2}-\\d{2}, Version ${versionForRegex} '${codename}' \\(LTS\\), @\\S+`);
+ title = new RegExp(`## \\d{4}-\\d{2}-\\d{2}, Version ${versionForRegex} '${RegExp.escape(codename)}' \\(LTS\\), @\\S+`);
} else {
title = new RegExp(`## \\d{4}-\\d{2}-\\d{2}, Version ${versionForRegex} \\(Current\\), @\\S+`);
}
@@ -70,20 +70,20 @@ const changelogPath = `doc/changelogs/CHANGELOG_V${major}.md`;
// Check for the link to the appropriate CHANGELOG_V*.md file.
let linkToChangelog;
if (lts) {
- linkToChangelog = new RegExp(`\\[Node\\.js ${major}\\]\\(${changelogPath}\\) \\*\\*Long Term Support\\*\\*`);
+ linkToChangelog = new RegExp(`\\[Node\\.js ${major}\\]\\(${RegExp.escape(changelogPath)}\\) \\*\\*Long Term Support\\*\\*`);
} else {
- linkToChangelog = new RegExp(`\\[Node\\.js ${major}\\]\\(${changelogPath}\\) \\*\\*Current\\*\\*`);
+ linkToChangelog = new RegExp(`\\[Node\\.js ${major}\\]\\(${RegExp.escape(changelogPath)}\\) \\*\\*Current\\*\\*`);
}
assert.match(mainChangelog, linkToChangelog);
// Check table header.
let tableHeader;
if (lts) {
- tableHeader = new RegExp(` | ${major} \\(LTS\\) | `);
+ tableHeader = new RegExp(`${major} \\(LTS\\) | `);
} else {
- tableHeader = new RegExp(`${major} \\(Current\\) | `);
+ tableHeader = new RegExp(`${major} \\(Current\\) | `);
}
assert.match(mainChangelog, tableHeader);
// Check the table contains a link to the release in the appropriate CHANGELOG_V*.md file.
- const linkToVersion = new RegExp(`${versionForRegex}
`);
+ const linkToVersion = new RegExp(`${versionForRegex}
`);
assert.match(mainChangelog, linkToVersion);
}
diff --git a/test/parallel/test-repl-custom-eval-previews.js b/test/parallel/test-repl-custom-eval-previews.js
index 303115dd6fdfb1..5fea4e3d8c5a31 100644
--- a/test/parallel/test-repl-custom-eval-previews.js
+++ b/test/parallel/test-repl-custom-eval-previews.js
@@ -45,7 +45,7 @@ describe('with previews', () => {
);
const lines = getSingleCommandLines(output);
assert.match(lines.command, /^'Hello custom' \+ ' eval World!'/);
- assert.match(lines.prompt, new RegExp(`${testingReplPrompt}$`));
+ assert.match(lines.prompt, new RegExp(`${RegExp.escape(testingReplPrompt)}$`));
assert.strictEqual(lines.result, "'Hello custom eval World!'");
assert.strictEqual(lines.preview, undefined);
});
@@ -62,7 +62,7 @@ describe('with previews', () => {
);
const lines = getSingleCommandLines(output);
assert.match(lines.command, /^'Hello custom' \+ ' eval World!'/);
- assert.match(lines.prompt, new RegExp(`${testingReplPrompt}$`));
+ assert.match(lines.prompt, new RegExp(`${RegExp.escape(testingReplPrompt)}$`));
assert.strictEqual(lines.result, "'Hello custom eval World!'");
assert.match(lines.preview, /'Hello custom eval World!'/);
});
diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js
index ae45615c2c65ac..3eac0b462d1c52 100644
--- a/test/parallel/test-util-inspect.js
+++ b/test/parallel/test-util-inspect.js
@@ -2916,7 +2916,7 @@ assert.strictEqual(
util.inspect(err, { colors: true }).split('\n').forEach(common.mustCallAtLeast((line, i) => {
let expected = stack[i].replace(/node_modules\/(@[^/]+\/[^/]+|[^/]+)/gi, (_, m) => {
return `node_modules/\u001b[4m${m}\u001b[24m`;
- }).replaceAll(new RegExp(`(\\(?${escapedCWD}(\\\\|/))`, 'gi'), (_, m) => {
+ }).replaceAll(new RegExp(`(\\(?${RegExp.escape(escapedCWD)}(\\\\|/))`, 'gi'), (_, m) => {
return `\x1B[90m${m}\x1B[39m`;
});
if (expected.includes(process.cwd()) && expected.endsWith(')')) {
diff --git a/test/parallel/test-watch-mode-kill-signal-default.mjs b/test/parallel/test-watch-mode-kill-signal-default.mjs
index 53aec43d22e050..c5323283be8bb5 100644
--- a/test/parallel/test-watch-mode-kill-signal-default.mjs
+++ b/test/parallel/test-watch-mode-kill-signal-default.mjs
@@ -63,5 +63,5 @@ child.on('message', (msg) => {
await once(child, 'exit');
-assert.match(stdout, new RegExp(`__SIGTERM received__ ${firstGrandchildPid}`));
-assert.doesNotMatch(stdout, new RegExp(`__SIGINT received__ ${firstGrandchildPid}`));
+assert.match(stdout, new RegExp(`__SIGTERM received__ ${RegExp.escape(firstGrandchildPid)}`));
+assert.doesNotMatch(stdout, new RegExp(`__SIGINT received__ ${RegExp.escape(firstGrandchildPid)}`));
diff --git a/test/parallel/test-watch-mode-kill-signal-override.mjs b/test/parallel/test-watch-mode-kill-signal-override.mjs
index dc3af3f76e848d..ca49b4880f90a3 100644
--- a/test/parallel/test-watch-mode-kill-signal-override.mjs
+++ b/test/parallel/test-watch-mode-kill-signal-override.mjs
@@ -67,5 +67,5 @@ await once(child, 'exit');
// The second grandchild, if there is one, could receive SIGTERM if it's killed as a
// consequence of the parent being killed in this process instead of being killed by the
// parent for file changes. Here we only care about the first grandchild.
-assert.match(stdout, new RegExp(`__SIGINT received__ ${firstGrandchildPid}`));
-assert.doesNotMatch(stdout, new RegExp(`__SIGTERM received__ ${firstGrandchildPid}`));
+assert.match(stdout, new RegExp(`__SIGINT received__ ${RegExp.escape(firstGrandchildPid)}`));
+assert.doesNotMatch(stdout, new RegExp(`__SIGTERM received__ ${RegExp.escape(firstGrandchildPid)}`));
diff --git a/test/parallel/test-whatwg-webstreams-encoding.js b/test/parallel/test-whatwg-webstreams-encoding.js
index 24d9bce7cc5212..588523fa788bc8 100644
--- a/test/parallel/test-whatwg-webstreams-encoding.js
+++ b/test/parallel/test-whatwg-webstreams-encoding.js
@@ -50,7 +50,7 @@ const kEuro = Buffer.from([0xe2, 0x82, 0xac]).toString();
() => Reflect.get(TextDecoderStream.prototype, getter, {}), {
name: 'TypeError',
message: /Cannot read private member/,
- stack: new RegExp(`at get ${getter}`)
+ stack: new RegExp(`at get ${RegExp.escape(getter)}`)
}
);
});
@@ -79,7 +79,7 @@ const kEuro = Buffer.from([0xe2, 0x82, 0xac]).toString();
() => Reflect.get(TextDecoderStream.prototype, getter, {}), {
name: 'TypeError',
message: /Cannot read private member/,
- stack: new RegExp(`at get ${getter}`)
+ stack: new RegExp(`at get ${RegExp.escape(getter)}`)
}
);
});