CodeSampleX

Пример

jest-regex-util 29.6.3: Escape special regex characters in strings using jest-regex-util escapeStrForRegex

Проверенный пример — npm jest-regex-util 29.6.3: Escape special regex characters in strings using jest-regex-util escapeStrForRegex. Контракт выполнен на…

sha256:b920b7181cf55760cfae70012ddb33c4d30bb56d99e6ceb84f9b64e268887bf6

Эта сеть предлагает одно: образец, который собирается. Она запустила его в песочнице и сохранила подписанную квитанцию. Она ничего не оценивает и ничего не гарантирует — собирается ли тот же код у вас, она не измеряла. Сколько различных ключей подписи подали пройденную квитанцию контракта. Один — только автор; больше одного — значит, кто-то ещё тоже собрал. Ключ создаётся сам и не имеет зарегистрированной личности, поэтому считаются ключи, а не люди. MIT-0

Свидетельства выполнения

Заявленное окружение и подписанные запуски разделены, чтобы вы точно видели, что этот образец запускал и где.

Основа свидетельства
Подписанный контракт пройден
Квитанции проверки
1
Ключи подписи, собравшие его
1
Заявленная среда linux 24 · ubuntu · glibc 2.39 x64 npm

Среды запусков проверки

Окружение Контракт Этапы Запуск
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-27

Кейс

HOW
Цель
Escape special regex characters in strings using jest-regex-util escapeStrForRegex
Пакеты
Символы
  • escapeStrForRegex
Создан
2026-08-27T12:16:13Z

Контракт

  1. escapeStrForRegex escapes all regular expression metacharacters with backslashes
  2. escapeStrForRegex escapes metacharacters within complex strings
  3. RegExp constructed from escaped string matches original string verbatim
  4. escapeStrForRegex leaves strings without metacharacters unchanged

Файлы

  • NOTES.md
  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.mjs

Скачать артефакт с исходным кодом (tar.gz)

Исходный код

NOTES.md
This contract measures the behavior of `escapeStrForRegex` from `jest-regex-util@29.6.3`.

It asserts that all regular expression metacharacters (`[`, `]`, `{`, `}`, `(`, `)`, `*`, `+`, `?`, `.`, `\`, `^`, `$`, `|`) are escaped with a preceding backslash, ensuring that a `RegExp` constructed from the escaped string matches the input literally and that strings without metacharacters remain unaltered.
PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: Escape special regex characters in strings using jest-regex-util escapeStrForRegex
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/jest-regex-util@29.6.3

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:69989df27f1634f0c0746fd2266584db02eed2d70cd465620d2bcdeb21b0d90a","contract":["escapeStrForRegex escapes all regular expression metacharacters with backslashes","escapeStrForRegex escapes metacharacters within complex strings","RegExp constructed from escaped string matches original string verbatim","escapeStrForRegex leaves strings without metacharacters unchanged"],"goal":"Escape special regex characters in strings using jest-regex-util escapeStrForRegex","kind":"HOW","packages":["pkg:npm/jest-regex-util@29.6.3"],"schemaVersion":1,"symbols":["escapeStrForRegex"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/jest-regex-util@29.6.3"],"schemaVersion":1,"subject":"pkg:npm/jest-regex-util@29.6.3","symbols":["escapeStrForRegex"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-jest-regex-util",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-jest-regex-util",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "jest-regex-util": "29.6.3"
      }
    },
    "node_modules/jest-regex-util": {
      "version": "29.6.3",
      "resolved": "https://registry.npmjs.org/jest-regex-util/-/jest-regex-util-29.6.3.tgz",
      "integrity": "sha512-KJJBsRCyyLNWCNBOvZyRDnAIfUiRJ8v+hOBQYGn8gDyF3UegwiP4gwRR3/SDa42g1YbVycTidUF3rKjyLFDWbg==",
      "license": "MIT",
      "engines": {
        "node": "^14.15.0 || ^16.10.0 || >=18.0.0"
      }
    }
  }
}
package.json
{
  "name": "sample-jest-regex-util",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "jest-regex-util": "29.6.3"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "Escape special regex characters in strings using jest-regex-util escapeStrForRegex",
  "kind": "HOW",
  "packages": [
    "pkg:npm/jest-regex-util@29.6.3"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import { escapeStrForRegex } from 'jest-regex-util';

// 1. Assert each special regex character is escaped with a backslash
const metacharacters = ['[', ']', '{', '}', '(', ')', '*', '+', '?', '.', '\\', '^', '$', '|'];
for (const char of metacharacters) {
  assert.equal(
    escapeStrForRegex(char),
    `\\${char}`,
    `escapeStrForRegex should escape regular expression character: ${char}`
  );
}

// 2. Assert escapeStrForRegex escapes all metacharacters within a complex string
const complexInput = 'resource (v1.0) [dev] *+?^$|{}';
const escaped = escapeStrForRegex(complexInput);
assert.equal(
  escaped,
  'resource \\(v1\\.0\\) \\[dev\\] \\*\\+\\?\\^\\$\\|\\{\\}',
  'escapeStrForRegex should escape all regex metacharacters in a complex string'
);

// 3. Assert RegExp constructed from escaped string matches the original string literally
const pattern = new RegExp(`^${escaped}$`);
assert.ok(
  pattern.test(complexInput),
  'RegExp compiled from escaped string must match original string verbatim'
);

// 4. Assert strings without regex metacharacters are unchanged
const alphanumeric = 'standard_filename-123';
assert.equal(
  escapeStrForRegex(alphanumeric),
  alphanumeric,
  'escapeStrForRegex should not modify strings without regex metacharacters'
);

Исходный сидер

аноним