热门角色不仅是灵感来源,更是你的效率助手。通过精挑细选的角色提示词,你可以快速生成高质量内容、提升创作灵感,并找到最契合你需求的解决方案。让创作更轻松,让价值更直接!
我们根据不同用户需求,持续更新角色库,让你总能找到合适的灵感入口。
本提示词专为JavaScript开发场景设计,能够根据用户提供的模式描述生成精确、高效的正则表达式解决方案。提示词采用专业开发者角色设定,确保生成的代码符合JavaScript最佳实践,同时提供详细的代码解释和使用示例。适用于表单验证、文本匹配、数据提取等多种Web开发场景,帮助开发者快速解决正则表达式编写难题。
// 推荐:先对输入做 trim,再整串匹配
const USERNAME_RE = /^(?!.*__)[A-Za-z][A-Za-z0-9_]{1,14}[A-Za-z0-9]$/;
function isValidUsername(input) {
const value = String(input).trim(); // 忽略首尾空格
return USERNAME_RE.test(value);
}
// 可选(一次性容忍首尾空格):直接在正则中处理首尾空白
// const USERNAME_RE_WITH_SPACES = /^\s*(?!.*__)[A-Za-z][A-Za-z0-9_]{1,14}[A-Za-z0-9]\s*$/;
// 使用方式与上面类似
正则分解(以 USERNAME_RE 为例):
关于忽略首尾空格:
console.log(isValidUsername('Alice_01')); // true
console.log(isValidUsername(' Bob_2 ')); // true(被 trim 后验证)
console.log(isValidUsername('ab')); // false(长度不足 3)
console.log(isValidUsername('1alex')); // false(未以字母开头)
console.log(isValidUsername('alex__wu')); // false(连续下划线)
console.log(isValidUsername('Kevin_')); // false(以下划线结尾)
console.log(isValidUsername('abcdefghijklmnop')); // true(16 个字母)
console.log(isValidUsername('abcdefghijklmnopq')); // false(17 个字符超长)
const inputEl = document.querySelector('#username');
const errorEl = document.querySelector('#username-error');
inputEl.addEventListener('input', () => {
if (isValidUsername(inputEl.value)) {
errorEl.textContent = '';
} else {
errorEl.textContent = '用户名需以字母开头,长度3-16,允许字母/数字/下划线,且不能有连续下划线或以下划线结尾。';
}
});
const rawList = [' Alice_01 ', 'ab', '1alex', 'ab__cd', 'JackMa', 'Jerry_'];
const cleanList = rawList.map(s => s.trim()).filter(s => USERNAME_RE.test(s));
console.log(cleanList); // ['Alice_01', 'JackMa']
// Global extractor: captures full order id, date, and sequence in groups 1–3
const ORDER_ID_RE = /(?:(?<=\[)|(?<=")|(?<=')|(?<![\w-]))(ORD-(\d{8})-(\d{6}))(?:(?=\])|(?=")|(?=')|(?![\w-]))/g;
// Example extractor function
function extractOrders(text) {
const results = [];
for (const m of text.matchAll(ORDER_ID_RE)) {
// m[1] = full order id, m[2] = date (YYYYMMDD), m[3] = sequence (6 digits)
results.push({
id: m[1],
date: m[2],
seq: m[3],
index: m.index, // start index of the match (the order id itself)
});
}
return results;
}
(?:(?<=\[)|(?<=")|(?<=')|(?<![\w-]))(ORD-(\d{8})-(\d{6}))(?:(?=\])|(?=")|(?=')|(?![\w-]))g (global): find all matches in the text.Breakdown:
(?:(?<=\[)|(?<=")|(?<=')|(?<![\w-]))
(?<=\[)|(?<=")|(?<='): If the ID is wrapped, assert it is immediately preceded by [, " or '.(?<![\w-]): Otherwise, ensure the ID is not glued to a word character or a hyphen on its left. This avoids partial matches inside longer tokens.(ORD-(\d{8})-(\d{6}))
ORD-YYYYMMDD-XXXXXX (the full order number).\d{8} (the date, 8 digits, no leap-year validation by design).\d{6} (the 6-digit sequence).(?:(?=\])|(?=")|(?=')|(?![\w-]))
(?=\])|(?=")|(?='): If wrapped, assert the matching closing ], " or ' immediately follows.(?![\w-]): Otherwise, ensure the right neighbor is not a word character or hyphen.Notes on wrapper handling:
[...], single quotes '...', or double quotes "...".const log = `
INFO created: ORD-20250101-000123
DEBUG payload: "ORD-20240229-123456" processed
WARN missing id 'ORD-20231231-654321'
OK list: [ORD-20240315-000001] and also ORD-20250101-888888 end
`;
const matches = extractOrders(log);
console.log(matches);
/*
[
{ id: 'ORD-20250101-000123', date: '20250101', seq: '000123', index: 15 },
{ id: 'ORD-20240229-123456', date: '20240229', seq: '123456', index: 39 },
{ id: 'ORD-20231231-654321', date: '20231231', seq: '654321', index: 83 },
{ id: 'ORD-20240315-000001', date: '20240315', seq: '000001', index: 117 },
{ id: 'ORD-20250101-888888', date: '20250101', seq: '888888', index: 153 }
]
*/
const ids = Array.from(log.matchAll(ORDER_ID_RE), m => m[1]);
console.log(ids);
// ['ORD-20250101-000123', 'ORD-20240229-123456', 'ORD-20231231-654321', 'ORD-20240315-000001', 'ORD-20250101-888888']
const only2025 = [];
for (const m of log.matchAll(ORDER_ID_RE)) {
if (m[2].startsWith('2025')) only2025.push({ id: m[1], date: m[2], seq: m[3] });
}
console.log(only2025);
// [{ id: 'ORD-20250101-000123', date: '20250101', seq: '000123' }, { id: 'ORD-20250101-888888', date: '20250101', seq: '888888' }]
?<=/?<!). Most modern engines (Node.js 10+, recent Chrome/Edge/Firefox/Safari) support them. If you must support environments without lookbehind, use a two-pass approach:
/ORD-(\d{8})-(\d{6})/g,[...], '...', or "..." contexts.], ' or ", or separated from word/hyphen characters. It does not strictly enforce that the opening and closing wrappers are the same character in a single, indivisible branch. In typical logs with well-formed wrappers this is sufficient for extraction. If strict pairing is required, consider a two-pass validation (as described above) or a parser that checks both sides explicitly.ORD-. If your logs may contain lowercase or mixed-case, add the i flag and optionally verify the prefix in code.// 路径:/users/:id/photos/:photoId?
// 要求:id 与 photoId 为数字;photoId 可选;可忽略末尾斜杠;整串匹配;依次捕获 id 与 photoId
// 推荐:使用位置捕获,确保依次捕获 id(第1组)与 photoId(第2组)
const routeRe = /^\/users\/(\d+)\/photos(?:\/(\d+))?\/?$/;
// 可选(增强可读性,同时仍保持捕获顺序 1:id, 2:photoId)
// const routeRe = /^\/users\/(?<id>\d+)\/photos(?:\/(?<photoId>\d+))?\/?$/;
function parsePath(path) {
const match = routeRe.exec(path);
if (!match) return null;
// 依次捕获:match[1] 为 id,match[2] 为 photoId(可能为 undefined)
const id = parseInt(match[1], 10);
const photoId = match[2] !== undefined ? parseInt(match[2], 10) : null;
return { id, photoId };
}
// 简单测试
console.log(parsePath('/users/123/photos/456')); // { id: 123, photoId: 456 }
console.log(parsePath('/users/123/photos')); // { id: 123, photoId: null }
console.log(parsePath('/users/123/photos/456/'));// { id: 123, photoId: 456 }
console.log(parsePath('/users/123/photos/')); // { id: 123, photoId: null }
console.log(parsePath('/users/abc/photos')); // null
匹配逻辑:
场景1:路由中间件参数解析
const path = '/users/987/photos/321';
const m = routeRe.exec(path);
if (m) {
const id = Number(m[1]);
const photoId = m[2] ? Number(m[2]) : null;
// 继续执行业务逻辑
}
场景2:统一解析函数(用于服务端或前端路由)
function matchUserPhotoPath(path) {
const m = routeRe.exec(path);
if (!m) return null;
return { id: Number(m[1]), photoId: m[2] ? Number(m[2]) : null };
}
console.log(matchUserPhotoPath('/users/42/photos')); // { id: 42, photoId: null }
console.log(matchUserPhotoPath('/users/42/photos/99/')); // { id: 42, photoId: 99 }
场景3:批量校验与提取
const paths = ['/users/1/photos', '/users/2/photos/3', '/users/abc/photos'];
const results = paths.map(p => {
const m = routeRe.exec(p);
return m ? { ok: true, id: +m[1], photoId: m[2] ? +m[2] : null } : { ok: false };
});
console.log(results);
// [{ok:true,id:1,photoId:null},{ok:true,id:2,photoId:3},{ok:false}]
用一条指令,快速产出“可直接上手”的正则方案,帮助前端/全栈在表单校验、文本提取、URL解析、日志处理与数据清洗等场景中,精准匹配、稳定上线。通过专业角色扮演与结构化输出,给到清晰的正则写法、运行示例与注意事项,减少试错与返工,显著缩短开发到交付的时间。
快速产出生/改/测表单校验规则,如注册登录、地址、支付信息;复制示例即用,减少线上因校验不严导致的问题。
统一团队校验口径,沉淀可复用模板;根据业务变化一键调整规则并分发,提高迭代速度与交付一致性。
自动生成命中与未命中样例,覆盖边界与异常输入;快速构造用例验证需求,提升缺陷发现率。
将模板生成的提示词复制粘贴到您常用的 Chat 应用(如 ChatGPT、Claude 等),即可直接对话使用,无需额外开发。适合个人快速体验和轻量使用场景。
把提示词模板转化为 API,您的程序可任意修改模板参数,通过接口直接调用,轻松实现自动化与批量处理。适合开发者集成与业务系统嵌入。
在 MCP client 中配置对应的 server 地址,让您的 AI 应用自动调用提示词模板。适合高级用户和团队协作,让提示词在不同 AI 工具间无缝衔接。
半价获取高级提示词-优惠即将到期