テストデータ
let data=`
Results update in real-time as you type.
Supports JavaScript & PHP/PCRE RegEx.
Roll over a match or expression for details.
Save & share expressions with others.
Use Tools to explore your results.
Browse the Reference for help & examples.
Undo & Redo with ctrl-Z / Y in editors.
Search for & rate Community patterns.
`;
まず幾つかパターン
let a=data.split(/\r?\n/).filter(d=>d.length!=0).slice(0,2).join('\n')
console.log(a)
let b=data.trim().split(/\r?\n/).slice(0,2).join('\n');
console.log(b)
let c=data.match(/.+\r?\n.*\r?\n/).join('')
console.log(c)
//=>Results update in real-time as you type.
//Supports JavaScript & PHP/PCRE RegEx.
たいてい上述の3つで事足りるが、データ数の見通しがない場合、splitの全分割やregexの*は一度最後までデータを読み取ってしまう。
データの小見出しが欲しい場合、常識の範囲内で先に切ると、一万文字くらいから速度差がでる。
let lim=1000,x=data.slice(0,lim)
let d=x.match(/.+\r?\n.*\r?\n/).join('')||x
console.log(d)