改行

2016-11-19 10 views
-4

を含む文字列を検索し、ラップ次の文字列を考えてみましょうスパンタグ?例えば改行

You look away whilst I weave needles 
<span>through my skin to seal the scars, 
running repairs, not alterations.</span> 

現在、私は持っている:任意の\r\nを含まない文字列のために正常に動作します

let regex = new RegExp(stringToMatch,'m'); //I thought 'm', multiline might work? 
poem = poem.replace(regex, '<span>' + stringToMatch + '</span>') 

答えて

0

は、私はあなたがこの

var poem = 'You look away whilst I weave needles\r\nthrough my skin to seal the scars,\r\nrunning repairs, not alterations.\r\n'; 
let regex = /((.+[\r\n]+)+)((.+[\r\n]+){2})/ 
poem = poem.replace(regex, '$1<span>$3</span>'); 
のようなものを使用することをお勧め