Javascript how to split newline

后端 未结 12 1812
一整个雨季
一整个雨季 2020-11-28 06:48

I\'m using jquery, and I have a textarea. When I submit by my button I will alert each text separated by newline. How to split my text when there is a newline?



        
相关标签:
12条回答
  • 2020-11-28 07:11

    You should parse newlines regardless of the platform (operation system) This split is universal with regular expressions. You may consider using this:

    var ks = $('#keywords').val().split(/\r?\n/);
    

    E.g.

    "a\nb\r\nc\r\nlala".split(/\r?\n/) // ["a", "b", "c", "lala"]
    
    0 讨论(0)
  • 2020-11-28 07:11

    Here is example with console.log instead of alert(). It is more convenient :)

    var parse = function(){
      var str = $('textarea').val();
      var results = str.split("\n");
      $.each(results, function(index, element){
        console.log(element);
      });
    };
    
    $(function(){
      $('button').on('click', parse);
    });
    

    You can try it here

    0 讨论(0)
  • 2020-11-28 07:14

    Since you are using textarea, you may find \n or \r (or \r\n) for line breaks. So, the following is suggested:

    $('#keywords').val().split(/\r|\n/)

    ref: check whether string contains a line break

    0 讨论(0)
  • 2020-11-28 07:19

    The simplest and safest way to split a string using new lines, regardless of format (CRLF, LFCR or LF), is to remove all carriage return characters and then split on the new line characters. "text".replace(/\r/g, "").split(/\n/);

    This ensures that when you have continuous new lines (i.e. \r\n\r\n, \n\r\n\r, or \n\n) the result will always be the same.

    In your case the code would look like:

    (function ($) {
        $(document).ready(function () {
            $('#data').submit(function (e) {
                var ks = $('#keywords').val().replace(/\r/g, "").split(/\n/);
                e.preventDefault();
                alert(ks[0]);
                $.each(ks, function (k) {
                    alert(k);
                });
            });
        });
    })(jQuery);
    

    Here are some examples that display the importance of this method:

    var examples = ["Foo\r\nBar", "Foo\r\n\r\nBar", "Foo\n\r\n\rBar", "Foo\nBar\nFooBar"];
    
    examples.forEach(function(example) {
      output(`Example "${example}":`);
      output(`Split using "\n": "${example.split("\n")}"`);
      output(`Split using /\r?\n/: "${example.split(/\r?\n/)}"`);
      output(`Split using /\r\n|\n|\r/: "${example.split(/\r\n|\n|\r/)}"`);
      output(`Current method: ${example.replace(/\r/g, "").split("\n")}`);
      output("________");
    });
    
    function output(txt) {
      console.log(txt.replace(/\n/g, "\\n").replace(/\r/g, "\\r"));
    }

    0 讨论(0)
  • 2020-11-28 07:28

    (function($) {
      $(document).ready(function() {
        $('#data').click(function(e) {
          e.preventDefault();
          $.each($("#keywords").val().split("\n"), function(e, element) {
            alert(element);
          });
        });
      });
    })(jQuery);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <textarea id="keywords">Hello
    World</textarea>
    <input id="data" type="button" value="submit">

    0 讨论(0)
  • 2020-11-28 07:29

    Good'ol javascript:

     var m = "Hello World";  
     var k = m.split(' ');  // I have used space, you can use any thing.
     for(i=0;i<k.length;i++)  
        alert(k[i]);  
    
    0 讨论(0)
提交回复
热议问题