2016-06-17 9 views
1

\nでテキストエリアの値を分割し、カーソルが配置されている行を配列の最後の値にしたいとします。最後の行としてカーソル位置のテキストエリアの値を分割する方法

1. fyg tgiyu rctvyu cuiby cutv cutrvyb crtvyb 
2. rutyu rtcvyb ctrvybu ctrvybu rtcvy 
3. rutiyu crtvyu crtvyb rtvyb 
4. | 
5. tgyho8uji vtybui 
6. tvybui yivtubi 

数字はテキストエリアの行で、行4はカーソルが置かれている行です。私は5行目と6行目を無視して行を分割したいので、最後の行は4行目です。

+0

何を試しましたか? – nicael

答えて

0

これは、jQuerysubstr()で実現できます。

開始点からカーソルがあるポイントまでのテキストエリア値の部分文字列を選択し、改行で分割します。このような;

value = $('textarea').val(); 
// to get the position of the cursor 
index = $('textarea').prop('selectionstart'); 
// select all from the starting point to the cursor position ignoring line 5 and 6 
str = value.substr(0, index); 
// split the str with a line break 
splt = str.split('\n'); 
// then finally to get your last line 
lastLine = splt[splt.length - 1]; 
0

$('#theTextArea').prop("selectionStart");

$('#theTextArea').prop("selectionEnd");

上記の特性

はあなたの場所を与える

lastLine = //the position of the cursor 
if(lastLine == ""){ 
    console.log('empty'); 
} else { 
    //get the value of the previous line before the lastLine 
} 

どのように私は、これはjQueryのまたはJavaScriptを使用して実現するようにしてください。そして、私はこのようなコードを実行します現代のブラウザ上のカーソル。この質問はlinked here before thoughです。古いブラウザの場合、問題がある可能性があります。

しかし、カーソルの位置を指定すると、部分文字列をカーソルインデックスにつかむだけでよいはずです。正規表現を使用して、最後の行の内容を文字列の最後に取得します。

関連する問題