Files
html-tools/tools/text-replacer/app.html
T
2025-12-23 14:12:36 +08:00

253 lines
7.3 KiB
HTML

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<link rel="icon" href="../../favicon.svg">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文本查找替换</title>
<link rel="canonical" href="https://www.htmls.dev/tools/text-replacer/app.html">
<style>
:root {
--accent: #8b5cf6;
--accent-soft: #8b5cf633;
--bg: #0f172a;
--text: #0f172a;
--text-muted: #475569;
--border: #e2e8f0;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #8b5cf6 0%, #0f172a 100%);
min-height: 100vh;
padding: 20px;
color: var(--text);
}
.container {
max-width: 1200px;
margin: 0 auto;
}
.header {
text-align: center;
color: white;
margin-bottom: 30px;
}
.header h1 {
font-size: 2.3rem;
margin-bottom: 8px;
}
.card {
background: rgba(255, 255, 255, 0.96);
border-radius: 18px;
padding: 26px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
gap: 16px;
}
.row {
display: flex;
gap: 12px;
flex-wrap: wrap;
align-items: center;
}
label {
font-size: 14px;
color: var(--text-muted);
}
input[type="text"],
input[type="number"],
input[type="url"],
input[type="file"],
select,
textarea {
width: 100%;
padding: 10px 12px;
border: 1px solid var(--border);
border-radius: 10px;
font-size: 14px;
background: white;
}
textarea {
min-height: 140px;
resize: vertical;
}
.actions {
display: flex;
gap: 10px;
flex-wrap: wrap;
margin-top: 14px;
}
.btn {
padding: 10px 18px;
border-radius: 10px;
border: none;
cursor: pointer;
font-size: 14px;
font-weight: 600;
background: var(--accent);
color: white;
transition: transform 0.2s, box-shadow 0.2s;
}
.btn:hover {
transform: translateY(-1px);
box-shadow: 0 8px 20px rgba(15, 23, 42, 0.2);
}
.btn-secondary {
background: #64748b;
}
.muted {
color: var(--text-muted);
font-size: 13px;
}
.output-box {
background: #f8fafc;
border: 1px dashed var(--border);
padding: 12px;
border-radius: 12px;
}
.preview {
width: 100%;
min-height: 180px;
border-radius: 12px;
background: #f1f5f9;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
}
.preview img {
max-width: 100%;
max-height: 300px;
display: block;
}
.chip {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 4px 10px;
border-radius: 999px;
background: var(--accent-soft);
color: var(--text);
font-size: 12px;
}
.result {
font-size: 18px;
font-weight: 700;
color: var(--accent);
}
.code {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace;
font-size: 13px;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>文本查找替换</h1>
<p>批量查找并替换文本内容</p>
</div>
<div class="card">
<div>
<label>原始文本</label>
<textarea id="input" placeholder="粘贴需要处理的文本"></textarea>
</div>
<div class="grid" style="margin-top: 16px;">
<div>
<label>查找内容</label>
<input type="text" id="findText" placeholder="输入要查找的文本">
</div>
<div>
<label>替换为</label>
<input type="text" id="replaceText" placeholder="替换内容">
</div>
</div>
<div class="row" style="margin-top: 12px;">
<label><input type="checkbox" id="ignoreCase"> 忽略大小写</label>
<label><input type="checkbox" id="useRegex"> 使用正则</label>
</div>
<div class="actions">
<button class="btn" id="replace">执行替换</button>
<button class="btn btn-secondary" id="copy">复制结果</button>
</div>
<div class="output-box" style="margin-top: 16px;">
<label>输出结果</label>
<textarea id="output" readonly placeholder="替换后的文本"></textarea>
<p class="muted">替换次数:<span id="count">0</span></p>
</div>
</div>
</div>
<script>
const input = document.getElementById('input');
const findText = document.getElementById('findText');
const replaceText = document.getElementById('replaceText');
const ignoreCase = document.getElementById('ignoreCase');
const useRegex = document.getElementById('useRegex');
const replaceBtn = document.getElementById('replace');
const copyBtn = document.getElementById('copy');
const output = document.getElementById('output');
const countEl = document.getElementById('count');
function escapeRegExp(value) {
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function replaceAll() {
const source = input.value;
const findValue = findText.value;
if (!findValue) {
output.value = source;
countEl.textContent = '0';
return;
}
const flags = ignoreCase.checked ? 'gi' : 'g';
const pattern = useRegex.checked ? findValue : escapeRegExp(findValue);
const regex = new RegExp(pattern, flags);
const matches = source.match(regex);
const result = source.replace(regex, replaceText.value);
output.value = result;
countEl.textContent = matches ? matches.length : '0';
}
replaceBtn.addEventListener('click', replaceAll);
copyBtn.addEventListener('click', () => {
if (!output.value) return;
output.select();
document.execCommand('copy');
});
</script>
<script src="/assets/clicks.js" defer></script>
</body>
</html>