-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