javascript - 仅包含以数字开头的行

标签 javascript html

我只想包含以序列号开头的行并删除所有其他行。这是我正在尝试的代码,请帮忙!请参阅图片以了解更多信息。

图片:/image/4dVCE.png

提前致谢

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/

相关文章:

javascript - 如何打印div中所选项目的文本

c# - 使用 HTML 5 和 ASP.NET MVC 5 的视频流

html - Google 字体特殊字符在 PC 上显示正确,但在 HTML 中却无法显示?

html - 我想制作一个卡住的标题

javascript - Html 表格转 PDF 我想对其进行格式化(页码,第二页不好)

jquery - 在线显示代码演示

javascript - node.js - 代码保护?

javascript - Bootstrap 数据表复选框更改类

javascript - AJAX 调用返回 json 数组未定义

javascript - 渲染中的箭头函数与。携带数据的属性