制作一允许用户输入文本和其中想要提取的文本,自动生成正则表达式的网站
- User
制作一允许用户输入文本和其中想要提取的文本,自动生成正则表达式的网站
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
制作一允许用户输入文本和其中想要提取的文本,自动生成正则表达式的网站
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
以下是一个允许用户输入文本和提取内容,自动生成正则表达式的简单网页示例。用户可以通过标记(如括号)指定要提取的部分,系统会自动生成对应的正则表达式。
() 标记要提取的部分。<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>正则表达式生成器</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; }
textarea { width: 100%; height: 100px; margin-bottom: 10px; }
#output { background: #f0f0f0; padding: 10px; white-space: pre-wrap; }
.test-section { margin-top: 20px; }
</style>
</head>
<body>
<h1>正则表达式生成器</h1>
<p>输入文本并用括号()标记要提取的部分:</p>
<textarea id="inputText" placeholder="例如:订单号:(123456),日期:(2023-10-05)"></textarea>
<button onclick="generateRegex()">生成正则表达式</button>
<div id="output"></div>
<div class="test-section">
<h2>测试正则表达式</h2>
<input type="text" id="testInput" placeholder="输入测试文本">
<button onclick="testRegex()">测试匹配</button>
<div id="testResult"></div>
</div>
<script>
function generateRegex() {
const input = document.getElementById('inputText').value;
const regexPattern = parseInput(input);
document.getElementById('output').textContent = `生成的正则表达式:${regexPattern}`;
}
function parseInput(text) {
const matches = text.match(/\((.*?)\)/g);
if (!matches) return '未找到可提取内容';
const patterns = matches.map(match => {
const content = match.slice(1, -1);
return generatePattern(content);
});
let result = text;
for (let i = 0; i < matches.length; i++) {
result = result.replace(matches[i], `(${patterns[i]})`);
}
return result;
}
function generatePattern(content) {
if (/^\d+$/.test(content)) {
return '\\d+';
} else if (/^\d{4}-\d{2}-\d{2}$/.test(content)) {
return '\\d{4}-\\d{2}-\\d{2}';
} else if (/^[A-Z][a-z]+\s+[A-Z][a-z]+$/.test(content)) {
return '[A-Z][a-z]+ [A-Z][a-z]+';
} else {
return '\\w+';
}
}
function testRegex() {
const regexText = document.getElementById('output').textContent;
const testText = document.getElementById('testInput').value;
const regex = new RegExp(regexText, 'g');
const matches = testText.match(regex);
const resultDiv = document.getElementById('testResult');
if (matches) {
resultDiv.textContent = '匹配结果:' + matches.join(', ');
} else {
resultDiv.textContent = '未找到匹配项';
}
}
</script>
</body>
</html>
输入文本:
订单号:(123456),日期:(2023-10-05)
生成的正则表达式:
订单号:(\d+),日期:(\d{4}-\d{2}-\d{2})
测试匹配:
订单号:678901,日期:2023-11-15匹配结果:678901, 2023-11-15此工具适合快速生成简单正则表达式,复杂场景需结合更高级的解析逻辑。