Hi, A verse is entered into the textarea. How to determine the position numbers of all line feeds in Javascript?

    2 answers 2

    var txt = document.getElementById('txtarea').value.replace('\r', ''); var txtar = txt.split('\n'); //а далее - работаем с txtar[i].length 

    Option adjusted for IE7 (thanks AlexWindHope):

     var txt = document.getElementById('txtarea').value.replace('\r', ''); var txtar = txt.split('\n'); //а далее - работаем с (txtar[i].indexOf('\r') ? txtar[i].length-1 : txtar[i].length) 
    • value.replace ('\ r', '') - does not work in some 7th IE (it sounds weird). The reason for this is that IE, as soon as the symbol clears itself, it itself - Zowie
    • And how many of these burly donkeys? If a lot, then you can immediately do split() , and then work with txtar[i].indexOf('\r')+1 ? txtar[i].length-1 : txtar[i].length txtar[i].indexOf('\r')+1 ? txtar[i].length-1 : txtar[i].length . - ling
    • Thank! works. Such an elegant solution. Please explain why you need to change the carriage transfer to emptiness? - Irinkes
    • @ling - on many whists by default. Not that now there are a lot of users of whists, but, actually, not so little - Zowie
    • one
      @Irinkes, this is due to a different way to wrap a line in Windows and Linux (or ASCII and Unicode, I don’t remember already). The fact is that somewhere it is encoded \r\n , and somewhere \n . @AlexWindHope, thanks for the comment. Now I will add the second option. - ling
     var txt = document.getElementById('txtarea').match(/[^\r]+/); nums = new Array(); pos = 0; for (i_line = 0; i_line < txt.length; i_line++) { pos += txt[i_line].length; nums.push((++pos) + ":" + txt[i_line]); }; // nums содержит номера позиций и строки разделенные знаком : 
    • thanks, I'll try to figure it out - Irinkes