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. contract 在 node 22 · linux alpine/x64…

sha256:b920b7181cf55760cfae70012ddb33c4d30bb56d99e6ceb84f9b64e268887bf6

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 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'
);

原始种子者

匿名