繁体   English   中英

通过遍历数组搜索文档中的关键字

[英]Searching for keywords in a document by looping through an array

我正在编写一个脚本,以读取和搜索上载到网站的文本文件中的关键字。 我有一个关于如何使用RegEx读取字符串并报告单词出现次数的想法,但是我无法弄清楚一旦加载了FileReader则如何访问文本。 我想通过FileReader完成工作后单击“开始编码”按钮来调用此脚本。 我创建了一个函数来完成这项工作,但是我不知道如何访问文件阅读器文本。 谢谢你的帮助!

我从这里获得了fileReader的脚本如何在JavaScript中读取文本文件

  <!DOCTYPE html>
<html>
  <head>
    <title>Read File (via User Input selection)</title>
    <button onclick="myFunction()">Start Coding</button>
    <script type="text/javascript">
    alert ("Please insert your text in the box below and we will begin searching")

    var reader; //GLOBAL File Reader object for demo purpose only

    /**
     * Check for the various File API support.
     */
    function checkFileAPI() {
        if (window.File && window.FileReader && window.FileList && window.Blob) {
            reader = new FileReader();
            return true; 
        } else {
            alert('The File APIs are not fully supported by your browser. Fallback required.');
            return false;
        }
    }

    /**
     * read text input
     */
    function readText(filePath) {
        var output = ""; //placeholder for text output
        if(filePath.files && filePath.files[0]) {           
            reader.onload = function (e) {
                output = e.target.result;
                displayContents(output);
            };//end onload()
            reader.readAsText(filePath.files[0]);
        }//end if html5 filelist support
        else if(ActiveXObject && filePath) { //fallback to IE 6-8 support via ActiveX
            try {
                reader = new ActiveXObject("Scripting.FileSystemObject");
                var file = reader.OpenTextFile(filePath, 1); //ActiveX File Object
                output = file.ReadAll(); //text contents of file
                file.Close(); //close file "input stream"
                displayContents(output);
            } catch (e) {
                if (e.number == -2146827859) {
                    alert('Unable to access local files due to browser security settings. ' + 
                     'To overcome this, go to Tools->Internet Options->Security->Custom Level. ' + 
                     'Find the setting for "Initialize and script ActiveX controls not marked as safe" and change it to "Enable" or "Prompt"'); 
                }
            }       
        }
        else { //this is where you could fallback to Java Applet, Flash or similar
            return false;
        }       
        return true;
    }   

    /**
     * display content using a basic HTML replacement
     */
    function displayContents(txt) {
        var el = document.getElementById('main'); 
        el.innerHTML = txt; //display output in DOM
    }   

    function myFunction() {
        alert("We will start by highlighting all the required search terms then we will being our search by looking for the instances of the word 'pilot'")
        var count = (txt.match(/program/g) || []).length

        alert ("your word occured " + count + " times.")
     }


</script>
</head>
<body onload="checkFileAPI();">
    <div id="container">    
        <input type="file" onchange='readText(this)' />
        <br/>
        <hr/>   
        <h3>Contents of the Text file:</h3>
        <div id="main">
            ...
        </div>
    </div>
</body>
</html>

更新资料

由于有了NinjaM和更多研究,我找到了如何连接到已加载到站点上的文档的方法。 我必须连接到ID,并使用.innerHTML方法连接上载的文档。 一切工作正常,除了我现在遇到使循环正常工作的新问题。

我想让用户将文档上载到此站点,然后让他们单击一系列在文档中搜索关键字的按钮。 我与一个组织有关气候变化的文档编码的组织合作,并且我试图加快这一过程。 通常,一个人通读文档,然后使用control-f搜索某些气候变化关键字。 如果关键字在正确的上下文中位于文档中,则用户在我们的数据库中对其进行注释。 该站点将使该过程自动化,并减少了人们不会搜索适当术语或不会搜索所有术语的某些风险。

我有一系列RegEx表达式,我想循环使用它们并在文档中进行搜索。 我制作了循环和表达式,但是我希望循环在数组中搜索完每个项目后重新开始。 例如,一旦它在文档中搜索了关键字pilot ,那么我希望它从头开始,并在整个文档中搜索关键字project 现在,搜索将在文档中寻找试点 ,然后一旦找到试点的最后一个实例,便进入项目 这是我希望帮助解决的部分代码: function windowFind(){ var pilotWords = ["pilot","project"]; for (var i = 0; i < pilotWords.length; i++) { ("Find -> are = " + window.find(pilotWords[i]))} } function windowFind(){ var pilotWords = ["pilot","project"]; for (var i = 0; i < pilotWords.length; i++) { ("Find -> are = " + window.find(pilotWords[i]))} }

您可以在github https://calebdow.github.io/上查看该网站的基本版本。您将必须找到上传测试文件

文本文件的内容将显示在

<div id="main">
    ...
</div>

或以纯文本格式运行时打开html文件。 至于使用Javascript中的文本,它将放置在代码中定义的名为output的变量中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM