search

Home  >  Q&A  >  body text

How to match empty lines with JavaScript regular expression

I have a string as follows.

var str = `
   hello
   world 
   
   hi
   good
`;

There is a blank line between world and hi. I want to split the blank line to get an array. How to achieve this? Tried this and it didn't work.
str.split(/^\r|\r\n|\n$/);

曾经蜡笔没有小新曾经蜡笔没有小新2809 days ago637

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-05-19 10:47:34

    var str = `
    hello
    world 
    
    hi
    good
    `;
    str.split(/\n\n|\r\r|\r\n\r\n/);

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-19 10:47:34

    var str = `
       hello
       world 
       
       hi
       good
    `;
    
    str.split(/\n\s*\n/g);

    reply
    0
  • Cancelreply