简体   繁体   English

javascript split(“ \\ n \\ r”)在空行上添加逗号

[英]javascript split(“\n\r”) add comma on empty line

I have below code which import file. 我下面的代码哪个导入文件。 I split it with split("\\n\\r") code but now every empty line has comma. 我用split(“ \\ n \\ r”)代码对其进行了拆分,但是现在每个空行都有逗号。 How to fix it.. See code below and out put picture. 如何解决。请参见下面的代码,然后放出图片。

<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
    </head>

<body>


        <textarea id="textareaid" name="textareaid" class = "textdata" value="" rows="10" cols="100" placeholder="Enter Any Text!"></textarea>
        <br>
        <input id="filename"  class="btnsubmit"  type="file" accept="text/plain" onchange="PreviewText();"  />

<script type="text/javascript">

              function PreviewText() {
                var file = document.getElementById("filename").files[0];
                    var reader = new FileReader();
                    reader.onload = function (e) {
                    var array = e.target.result.split("\n\r");
                    alert (array);
                    document.getElementById("textareaid").value = array;
                    };
                    reader.readAsText(file);
        };

</script>

</body>
</html>

在此处输入图片说明

You can filter all the lines which have only comma before displaying it in the textarea. 您可以过滤所有只有逗号的行,然后在文本区域中显示它。

check the below code snippet. 检查以下代码段。 I have used Array.filter to check if the line has , or not. 我已经使用Array.filter来检查线路有,还是没有。

 var array = [ "Hello", ",", "Something", ",", "Great" ]; array = array.filter(function(line) { return line.length > 1 && line !== ","; }); console.log(array); 

Why don't you do this, Array.join(" ") transform an array into a string and remove the separator, which in this case for arrays are commas 为什么不这样做, Array.join(" ")将数组转换为字符串并删除分隔符,在这种情况下,数组是逗号

Check it out : 看看这个 :

          function PreviewText() {
                var file = document.getElementById("filename").files[0];
                var reader = new FileReader();
                reader.onload = function (e) {

                var array = e.target.result.split("\n\r");
                document.getElementById("textareaid").value = array[0]
                document.getElementById("textareaid1").value = array[1]
                document.getElementById("textareaid2").value = array[2]
                };
                reader.readAsText(file);  // 
    };

========================================== // ========================== ================================================= ====================

If you want to do this dynamically, you can do like this: 如果要动态执行此操作,可以执行以下操作:

array.forEach(function(article){
  list = document.createElement("ul");
  line = document.createElement("li");
  span = document.createElement("span");

  art = document.createTextNode(article) 

  span.appendChild(art);
  line.appendChild(span);
  list.appendChild(line);

  boxNode.appendChild(list)
})

Example working (proof of concept) https://jsbin.com/fuyirajoni/edit?html,js,output 示例工作(概念验证) https://jsbin.com/fuyirajoni/edit?html,js,output

Hope it helps you :) 希望它能对您有所帮助:)

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

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