我只想包含以序列号开头的行并删除所有其他行。这是我正在尝试的代码,请帮忙!请参阅图片以了解更多信息。
提前致谢
Jsfiddle:https://jsfiddle.net/PriyankaJain/gnwhjbyf/
<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<html>
<body>
<h10 style="color: red;">*Write/paste your text, then click on "Convert" button, this will only include sequence number lines and remove all other lines. Now you can copy your text by clicking "Copy Text!" button. You can reset everything by clicking "Clear All!" button.</h10>
<center>
<form>
<textarea autocomplete="off" cols="30" id="TextInput1" name="message" rows="10" style="border: 3px solid #73AD21; width: 40%;"></textarea>
<textarea autocomplete="off" cols="30" id="TextInput2" name="message" rows="10" style="border: 3px solid #73AD21; width: 40%;"></textarea>
<input id="WhiteSpaceRemove" onclick="myFunction()" style="border: 3px solid #73AD21;" type="button" value="Convert" />
<input id="ClearAll" onclick="ClearAllText()" style="border: 3px solid #73AD21;" type="button" value="Clear All!" />
<input id="CopyButton" onclick="CopyFunction()" style="border: 3px solid #73AD21;" type="button" value="Copy Text!" /><br />
</form>
</center>
<script>
function ClearAllText() {
document.getElementById("TextInput1").value = "";
document.getElementById("TextInput2").value = "";
}
function myFunction() {
var x = document.getElementById("TextInput1").value.replace(/\r?\n|\r/g, ", ");
document.getElementById("TextInput2").value.match(/^\d/) = x;
document.getElementById("CopyButton").value="Copy Text!";
}
function CopyFunction() {
var copyText = document.getElementById("TextInput2");
copyText.select();
document.execCommand("copy");
document.getElementById("CopyButton").value="Copied";
}
</script>
</body>
</html>
</div>
<b>Tags: </b></div>
</div>
[1]:
最佳答案
这样可以吗?这主要是您的代码,我只更改了 myFunction
。
function ClearAllText() {
document.getElementById("TextInput1").value = "";
document.getElementById("TextInput2").value = "";
}
function myFunction() {
const x = document.getElementById("TextInput1").value.replace(/\r?\n|\r/g, '\n');
const lines = x.split('\n');
const numberedLines = lines.filter(line => /^\d/.test(line));
document.getElementById("TextInput2").value = numberedLines.join('\n');
}
function CopyFunction() {
var copyText = document.getElementById("TextInput2");
copyText.select();
document.execCommand("copy");
document.getElementById("CopyButton").value = "Copied";
}
<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<html>
<body>
<h10 style="color: red;">*Write/paste your text, then click on "Convert" button, this will only include sequence number lines and remove all other lines. Now you can copy your text by clicking "Copy Text!" button. You can reset everything by clicking "Clear All!" button.</h10>
<center>
<form>
<textarea autocomplete="off" cols="30" id="TextInput1" name="message" rows="10" style="border: 3px solid #73AD21; width: 40%;"></textarea>
<textarea autocomplete="off" cols="30" id="TextInput2" name="message" rows="10" style="border: 3px solid #73AD21; width: 40%;"></textarea>
<input id="WhiteSpaceRemove" onclick="myFunction()" style="border: 3px solid #73AD21;" type="button" value="Convert" />
<input id="ClearAll" onclick="ClearAllText()" style="border: 3px solid #73AD21;" type="button" value="Clear All!" />
<input id="CopyButton" onclick="CopyFunction()" style="border: 3px solid #73AD21;" type="button" value="Copy Text!" /><br />
</form>
</center>
</body>
</html>
</div>
<b>Tags: </b></div>
</div>
关于javascript - 仅包含以数字开头的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60067791/