首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript:如何在一个很长的段落中选择特定文本的哪个实例(例如,一个在段落中出现不止一次的单词)?

Javascript:如何在一个很长的段落中选择特定文本的哪个实例(例如,一个在段落中出现不止一次的单词)?
EN

Stack Overflow用户
提问于 2014-10-11 07:15:17
回答 2查看 115关注 0票数 2

我计划开发一个网络应用程序。在应用程序的一部分中,用户从段落中选择文本,然后单击“保存”按钮。

例如,用户从以下文本中选择"apple“(以粗体显示):

蔷薇属()是蔷薇科(Rosaceae)苹果树( Malus )的柚类果实。它是栽培最广的果树之一,也是人类使用的许多苹果树属成员中最广为人知的一种。家蝇树通常被简单地称为苹果树。

当用户单击保存按钮时,JS应该将其作为键值对添加到对象中。值应该是选定的文本(在本例中是apple),而键应该是指示所选文本的哪个实例的东西。原因是“苹果”再次出现在给定段落的最后一个词中。

类似于:

代码语言:javascript
复制
var object = new Object();
var textString = window.getSelection().toString();
object.indicator = textString;

我希望跟踪用户选择的"apple“实例(即选定的文本)。所以有可能保留它的范围吗?

接下来的步骤是存储这个对象,以便当用户再次启动这个页面或返回到这里时,我们告诉他他已经选择了什么。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-11 07:57:21

这个例子没有得到选择了哪个实例(第一个还是第二个),但是它确实得到了字符串中的偏移索引,这应该就足够了。

代码语言:javascript
复制
<div id="content">An apple is the pomaceous fruit of the Malus domestica tree of the rose family (Rosaceae). It is one of the most widely cultivated tree fruits, and the most widely known of the many members of genus Malus that are used by humans. Malus domestica tree is often simple called as an apple tree.</div>

<button onclick="saveSelection()">Save Selection</button>

<script>
    function saveSelection() {
        var selection = window.getSelection();
        var selectedText = selection.toString();
        var selectionInfo = {
            offset: selection.anchorOffset,
            length: selectedText.length,
            text: selectedText
        };
        localStorage["lastSelection"] = JSON.stringify(selectionInfo);
    }

    window.onload = function () {
        var selectionJSON = localStorage["lastSelection"];
        if (selectionJSON) {
            selection = JSON.parse(selectionJSON);
            alert(JSON.stringify(selection) + "\n" + document.getElementById("content").innerText.substr(selection.offset, selection.length));
        }
    };
</script>
票数 3
EN

Stack Overflow用户

发布于 2014-10-11 08:20:40

要获得哪个实例,可以使用regex来匹配以前的实例,并获得有多少个实例。

代码语言:javascript
复制
var text = "An apple is the pomaceous fruit of the Malus domestica tree of the rose family (Rosaceae). It is one of the most widely cultivated tree fruits, and the most widely known of the many members of genus Malus that are used by humans. Malus domestica tree is often simple called as an apple tree.";

var selection = window.getSelection();
var textString = selection.toString();
var previousText = text.substr(0, Math.max(selection.anchorOffset, selection.focusOffset));

//Escape special regex characters, http://stackoverflow.com/a/6969486/3492895
var textRegex = textString.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");

//Matches all instances, including current one
var instance = previousText.match(new RegExp(textRegex, "g")).length;

alert("Instance: " + instance);

工作示例:http://testnaman.neocities.org/quicktest6.html

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26312116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档