将textarea句子拆分为数组,并找出keyup()上哪个句子发生了变化 [英] Splitting textarea sentences into array and finding out which sentence changed on keyup()

查看:153
本文介绍了将textarea句子拆分为数组,并找出keyup()上哪个句子发生了变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本区域,其中包含以下内容:

I have a textarea with this content:

<textarea id="text">
Hello! Who am I? This is the third sentence. This, is another sentence.
</textarea>

由于这是一个文本区域,因此可以集中显示.我正在尝试找到一种方法:

Since it's a textarea, it can be focused. I'm trying to find a way to:

  1. 将文本区域内的所有句子分隔为一个javascript数组.我尝试了一种 $('#text').val().split()方法,但是在我的示例中不止一个句子分隔符.

  1. Separate all sentences inside the textarea into a javascript array. I tried a $('#text').val().split() approach, but there is more than just one sentence separator in my example.

我还需要知道光标当前所在的句子.

I also need to know in which sentence the cursor is currently positioned on.

为什么,你可能会问?用户更改内容时,我需要处理(使用按字符收费的付费API)语句.如果我仅对第二句话做些小改动,为什么要提交第一,第三和第四句话?当然,只提交第二个就便宜得多.

Why, you may ask? I need to process (with a paid API that charges per character) sentences when the user changes their content. If I only make a small change in the 2nd sentence, why should the 1st, 3rd and 4th sentences be submitted? It is of course much less expensive to only submit the 2nd one.

正如我之前说的,如果人们仅使用点分隔句子,但是.split()方法将是完美的,但是还应该考虑其他字符(!?.)-关于第二点,我发现了一个用于查找脱字符位置的函数:

As I previously said, the .split() approach would be perfect if people only used dots to separate sentences, but there are other characters that should also be considered (!?.) -- about the second point, i found a function to find de caret position:

function doGetCaretPosition (ctrl) {
    var CaretPos = 0;
    if (document.selection) {
        ctrl.focus ();
        var Sel = document.selection.createRange ();
        Sel.moveStart ('character', -ctrl.value.length);
        CaretPos = Sel.text.length;
    } else if (ctrl.selectionStart || ctrl.selectionStart == '0') {
        CaretPos = ctrl.selectionStart;
    }
    return (CaretPos);
}

问题是,该职位如何帮助我找到句子编号?欢迎所有想法!

The question is, how does the position help me find the sentence number? All ideas are welcome!

推荐答案

我看到的最简单的方法是像您提到的那样对str进行split().根据我的理解(并且没有找到对此的任何引用),jQuery实际上并没有split()函数,但是:

The easiest way I can see to do this is to split() the str like you mentioned. From my understanding (and not finding any references to this), jQuery doesn't actually have a split() function, however:

var str = $('#text').val();
var sentences = str.split(/[.|!|?]\s/gi);

因此,如您所见,javascript的split函数允许使用正则表达式.但是正则表达式并不是完美的(已经晚了,明天我会研究更好的正则表达式).

So as you can see, javascript's split function allows for regular expressions. The regex however isn't perfect (it's getting late, i'll look into getting a better one tomorrow).

一旦有了句子,只需要计算每个字符串的长度,直到获得插入符号的位置即可.因此得到第一句话的长度,=到插入号的位置是否更大,如果不是,则得到第二个字符串+第一个字符串的长度,等于或更大.如果等于或大于,则表示您正在使用的句子,使用的索引是句子编号.

Once you have the sentences it's just a matter of counting the length of each string until you get the caret's position. So get length of first sentence, is it bigger of = to the caret position, if not, get length of second string + first string, is that = or bigger. If it is equal or bigger, then that's the sentence you're on and the index your using is the sentence number.

这篇关于将textarea句子拆分为数组,并找出keyup()上哪个句子发生了变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆