minimax-m1 LMArena Human Preference 140K
制作一允许用户输入文本和其中想要提取的文本,自动生成正则表达式的网站
- User
制作一允许用户输入文本和其中想要提取的文本,自动生成正则表达式的网站
- Assistant
以下是实现该功能的完整HTML代码,包含自动生成正则表达式和测试功能:
<!DOCTYPE html> <html> <head> <title>正则表达式生成器</title> <style> body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; line-height: 1.6; max-width: 800px; margin: 20px auto; padding: 20px; background: #f5f5f5; } .container { background: white; padding: 30px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); } h1 { color: #2c3e50; text-align: center; margin-bottom: 30px; } .input-group { margin-bottom: 20px; } textarea { width: 100%; height: 120px; padding: 10px; border: 2px solid #ddd; border-radius: 5px; font-family: monospace; resize: vertical; } button { background: #3498db; color: white; border: none; padding: 12px 25px; border-radius: 5px; cursor: pointer; font-size: 16px; transition: background 0.3s; } button:hover { background: #2980b9; } .result { margin-top: 25px; padding: 15px; background: #e8f4f8; border-radius: 5px; border-left: 4px solid #3498db; } .test-section { margin-top: 30px; padding-top: 20px; border-top: 2px solid #eee; } .highlight { color: #e74c3c; font-weight: bold; } </style> </head> <body> <div class="container"> <h1>智能正则表达式生成器</h1> <div class="input-group"> <label><strong>示例文本:</strong></label> <textarea id="exampleText" placeholder="输入包含目标文本的示例,例如:订单号:MIN-20231005-X"></textarea> </div> <div class="input-group"> <label><strong>要提取的部分:</strong></label> <textarea id="extractText" placeholder="输入需要提取的文本片段,例如:20231005"></textarea> </div> <div style="text-align: center"> <button onclick="generateRegex()">生成正则表达式</button> </div> <div class="result"> <h3>生成的正则表达式:</h3> <pre id="regexResult">点击生成按钮查看结果</pre> </div> <div class="test-section"> <h3>测试匹配:</h3> <textarea id="testText" placeholder="输入需要测试的文本"></textarea> <div style="text-align: center; margin-top: 10px"> <button onclick="testRegex()">测试匹配</button> </div> <p id="testResult" style="margin-top: 15px"></p> </div> </div> <script> function generateRegex() { const example = document.getElementById('exampleText').value.trim(); const extract = document.getElementById('extractText').value.trim(); if (!example || !extract) { alert('请填写示例文本和要提取的部分!'); return; } // 分析提取文本的特征 let regexPattern; if (/^\d+$/.test(extract)) { regexPattern = '\\d+'; } else if (/^[A-Za-z]+$/.test(extract)) { regexPattern = '[A-Za-z]+'; } else if (/^[A-Za-z0-9]+$/.test(extract)) { regexPattern = '[A-Za-z0-9]+'; } else { // 默认转义特殊字符 regexPattern = extract.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } // 查找提取文本在示例中的位置 const index = example.indexOf(extract); if (index === -1) { document.getElementById('regexResult').innerHTML = '<span style="color: #e74c3c">错误:提取文本不在示例中</span>'; return; } // 获取上下文 const before = example.substring(0, index); const after = example.substring(index + extract.length); // 转义特殊字符 const escapeRegExp = (str) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const escapedBefore = escapeRegExp(before); const escapedAfter = escapeRegExp(after); // 生成正则表达式 const regex = `${escapedBefore}(${regexPattern})${escapedAfter}`; document.getElementById('regexResult').textContent = regex; } function testRegex() { const regexStr = document.getElementById('regexResult').textContent; const testText = document.getElementById('testText').value.trim(); if (!regexStr || regexStr.includes('错误')) { document.getElementById('testResult').innerHTML = '<span class="highlight">请先生成正则表达式!</span>'; return; } if (!testText) { document.getElementById('testResult').innerHTML = '<span class="highlight">请输入测试文本!</span>'; return; } try { const regex = new RegExp(regexStr, 'g'); const matches = testText.match(regex); if (matches) { document.getElementById('testResult').innerHTML = `<strong>匹配成功!</strong> 找到 ${matches.length} 处匹配:<br>` + matches.map(m => `<code>${m}</code>`).join('<br>'); } else { document.getElementById('testResult').innerHTML = '<span style="color: #e74c3c">未找到匹配</span>'; } } catch (e) { document.getElementById('testResult').innerHTML = '<span style="color: #e74c3c">无效的正则表达式</span>'; } } </script> </body> </html>功能说明:
-
输入区域:
- 示例文本:输入包含目标文本的完整示例
- 要提取的部分:输入需要提取的文本片段
-
生成逻辑:
- 自动分析提取文本的特征(数字/字母/混合)
- 根据上下文生成包含捕获组的正则表达式
- 支持特殊字符转义
-
测试功能:
- 输入测试文本验证正则表达式
- 显示所有匹配结果
- 实时反馈匹配状态
-
界面特性:
- 响应式布局
- 现代简洁的UI设计
- 友好的错误提示
使用示例:
-
示例输入:
- 示例文本:
订单号:MIN-20231005-X - 要提取的部分:
20231005
- 示例文本:
-
生成结果:
订单号:MIN-(\d+)-X -
测试用例:
- 输入测试文本:
产品编号:MIN-20240515-P - 匹配结果:
20240515
- 输入测试文本:
该实现能智能识别数字、字母等常见模式,并生成精确匹配上下文结构但保持核心部分灵活的正则表达式。用户可以通过测试功能快速验证生成结果。
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.