简体   繁体   English

使用Javascript从一个文本区域逐行分析substr(0,6)到另一个文本区域

[英]Parsing from one textarea line by line.substr(0,6) into another text area using Javascript

Originally this started with me trying to parse a text file upload through an input field but everything I researched came up with HTML5 and I don't want to use that. 最初,这是从我尝试解析通过输入字段上传的文本文件开始的,但是我研究的所有内容都是HTML5附带的,我不想使用它。 So then I switched to just pasting text into a textarea(input) and having Javascript parse it grabbing the first 16 characters to place in textarea (output1) and grabbing the first 6 characters to place in textarea (output2). 因此,然后我切换到仅将文本粘贴到textarea(输入)中,并用Javascript对其进行解析,以获取前16个字符放入textarea(输出1)并获取前6个字符放入textarea(输出2)。 When I do this it only parses the first line. 当我这样做时,它仅解析第一行。 I am sure this can be done with a loop but I haven't quite mastered loops yet. 我确信可以通过循环来完成,但是我还没有完全掌握循环。

The data I paste in would be like: 我粘贴的数据如下:

A2C6F8-008CFF294 A2C6F8-008CFF294
C4F2D1-008CAB312 C4F2D1-008CAB312

So output1 should have the first six, and output2 should have the whole line in these examples. 因此,在这些示例中,output1应该具有前六个,而output2应该具有整行。

Here is the code I have. 这是我的代码。 Not sure what the next steps are. 不知道下一步是什么。

function trimit() {
    var str = "";
    str = document.getElementById("input").value;
    var six = str.substr(0,6);
    var sixteen = str.substr(0,16);
    document.getElementById("output1").value = six;
    document.getElementById("output2").value = sixteen; }

<table>
        <tr>
          <td><div align="center"><textarea name="input" cols="25" rows="10" id="input" onchange="trimit();"></textarea></div></td>
          <td><div align="center"><textarea name="output1" cols="7" rows="10" id="output1"></textarea></div></td>
          <td><div align="center"><textarea name="output2" cols="20" rows="10" id="output2"></textarea></div></td>
        </tr>
</table>

I realize that there is redundancy in my javascript. 我意识到我的JavaScript中存在冗余。 I tend to do that when I am troubleshooting to make sure I don't miss something. 当我进行故障排除时,我倾向于这样做以确保我不会错过任何东西。 After I solve this I am going to take the same input, substr 6 characters and then change all of the line breaks into commas so I can submit it to a database query. 解决此问题后,我将使用相同的输入,将6个字符减去6个字符,然后将所有换行符更改为逗号,以便可以将其提交给数据库查询。

This is my first time posting here so I apologize if I missed something you wanted. 这是我第一次在这里发布信息,如果我错过了你想要的东西,我深表歉意。 I have read thousands of SO questions and should know what to put. 我已经阅读了成千上万个SO问题,应该知道该怎么办。

Update: I was going to add a picture but apparently I am not worthy yet so here is an ASCII model. 更新:我本来要添加图片,但是显然我不值得,所以这里是一个ASCII模型。

+---------INPUT---------+   +-----OUTPUT1-----+   +-------OUTPUT2-------+
| A2C6F8-008CFF294      |   | A2C6F8          |   | A2C6F8-008CFF294    |
| C4F2D1-008CAB312      |   | C4F2D1          |   | C4F2D1-008CAB312    |
|                       |-->|                 |-->|                     |
|                       |   |                 |   |                     |
|                       |   |                 |   |                     |
+-----------------------+   +-----------------+   +---------------------+

If I'm understanding correctly, you want the first 16 characters of each input line to show up in one of your output boxes and just the first 6 characters of the same to show up in the other. 如果我理解正确,则希望每个输入行的前16个字符显示在一个输出框中,而只将其同一行的前6个字符显示在另一个输出框中。

You can use regular expressions and get what you want. 您可以使用正则表达式并获得所需的内容。 So, first as an illustration: 因此,首先作为说明:

"A2C6F8-008CFF294\nC4F2D1-008CAB312".match(/^....../mg) // match first 6 of each line
      ==> ["A2C6F8", "C4F2D1"]
"A2C6F8-008CFF294\nC4F2D1-008CAB312".match(/^................/mg) // match first 16 of each line
      ==> ["A2C6F8-008CFF294", "C4F2D1-008CAB312"]

So your function would be: 因此,您的功能将是:

function trimit() {
    var str = "";
    str = document.getElementById("input").value;  
    var sixes = str.match(/^....../mg);
    var sixteens = str.match(/^................/mg);  
    document.getElementById("output1").value = sixes.join('\n');
    document.getElementById("output2").value = sixteen.join('\n'); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM