<!DOCTYPE html>
<html>
<head>
<title>每隔第几个替换</title>
<script>
function replaceNth() {
var inputText = document.getElementById("inputText").value; // 获取输入的文本
var searchText = document.getElementById("searchText").value; // 获取要替换的内容
var replaceText = document.getElementById("replaceText").value; // 获取替换后的内容
var n = parseInt(document.getElementById("nthText").value); // 获取每隔第几个
var count = 0;
var replacedText = inputText.replace(new RegExp(searchText, "g"), function(match) {
count++;
if (count % n === 0) {
return replaceText;
} else {
return match;
}
});
document.getElementById("outputText").textContent = replacedText; // 显示替换后的文本
}
</script>
</head>
<body>
<label for="inputText">输入文本:</label>
<textarea id="inputText" rows="5" cols="50"></textarea>
<br>
<label for="searchText">要替换的内容:</label>
<input type="text" id="searchText">
<br>
<label for="replaceText">替换后的内容:</label>
<input type="text" id="replaceText">
<br>
<label for="nthText">每隔第几个:</label>
<input type="number" id="nthText">
<br>
<button onclick="replaceNth()">替换</button>
<br>
<label for="outputText">替换结果:</label>
<textarea id="outputText" rows="5" cols="50" readonly></textarea>
</body>
</html>