showing results for - "js window selection get new line"
Alejandra
05 Sep 2018
1var userSelection
2function countLineBreaks(){
3
4  //Textarea
5  var textarea = document.getElementById("textArea");  
6
7  //Get user selection
8  userSelection = (textarea.value).substring(textarea.selectionStart,textarea.selectionEnd);
9  
10  //Simple regex to check for line break
11  var matches = userSelection.match(/\n/g);
12
13  //Count line breaks
14  var lineBreaksCount = matches ? matches.length : 0
15   
16  console.log(lineBreaksCount + ' Line breaks')
17}
18