-1
我需要獲取textarea中匹配的數量,但此數字在用戶正在鍵入之前。光標之前的匹配數(或位置用戶正在輸入之前)
例如:
@josh @marie @josh @anne <user is typing here> @josh @marie
假設我想@josh,我需要有2個在我的變量,因爲我在光標之前有兩個josh
/用戶鍵入之前。
任何想法如何得到它?
我嘗試:
var p = 'josh';
var regEx = new RegExp(p,"g");
$("#comment").on("keyup",function() {
var number = $('#comment').val().match(regEx).length;
$("#write").html(number);
});
,但我只能得到總場比賽,光標前不僅火柴/用戶之前輸入。
HTML:
<textarea id=comment>@josh @marie @anne @josh @anne @marie @chloe @josh @anne @josh @marie @anne @josh @anne @marie @chloe @josh @anne</textarea>
<br>
<span id=write></span> matches
https://jsfiddle.net/kstbd2a1/
你需要做一些錯誤就可以了檢查(因爲移動到輸入的開始離開VAL爲空,不喜歡被切片),但它可以作爲一個起點。 – temporalslide