私は、ウェブサイトにアップロードされたテキストファイルのキーワードを読み込んで検索するためのスクリプトを作成しています。文字列を読み込んでRegExを使って単語の出現回数を報告する方法について考えていますが、読み込まれた後はfileReaderからテキストにアクセスする方法を理解できません。 FileReaderが作業を終えた後、 "Start Coding"ボタンをクリックして、このスクリプトを呼び出したいと思います。私はこの作業を行うための関数を作成しましたが、ファイルリーダーのテキストにアクセスする方法を理解できません。ご協力いただきありがとうございます!配列をループして文書内のキーワードを検索する
私は、サイト上にNinjaMといくつかのより多くの研究のおかげでロードされた文書に接続する方法を考え出し更新ここHow to read text file in 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>
から
をFileReaderのためのスクリプトを得ました。 IDに接続し、.innerHTMLメソッドを使用してアップロードしたドキュメントに接続しなければなりませんでした。ループが正しく機能するようにするための新しい問題があることを除いて、すべてうまくいっています。
ユーザーがこのサイトにドキュメントをアップロードして、ドキュメント内のキーワードを検索する一連のボタンをクリックさせたいと考えています。私は気候変動に関する文書をコーディングしている組織と協力し、プロセスをスピードアップしようとしています。一般的に、ある人が文書を読んだ後、のcontrol-fを使用して、特定の気候変動のキーワードを検索します。キーワードが適切な文脈で文書内にある場合、ユーザはデータベース内でキーワードを書き留めます。このサイトは、そのプロセスを自動化し、人々が適切な用語を検索しない、またはすべての用語を検索しないリスクを減らします。
私は一連のRegEx式をループ内に置いて、ドキュメント内で検索したいと思っています。私はループと式を作ったが、配列内の各アイテムを検索した後、私のループがやり直したい。たとえば、キーワードのパイロットの文書を検索したら、最初からもう一度検索し、キーワードの文書を検索したいと思います。今すぐ検索するとパイロットがドキュメント内にあり、最後にパイロットの最後のインスタンスが見つかると、プロジェクトに移動します。これは私が助けを修正したいコードの一部です: 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/上のサイトの基本的なバージョンをチェックアウトすることができますが、テストファイル
'function myFunction(){alert("必要なすべての検索用語を強調表示してから、 'program'という単語のインスタンスを検索して検索します)var count =(output.match(/プログラム/ g)|| [])。長さの警告(あなたの単語が発生+ "+カウント+"回。) ' –
ありがとうNinjaM!私は自分の問題を調べており、関数の外のテキストの変数にアクセスする方法を理解する必要があると思っています。私はそれをどうやって行うのか分からない。どのように上のコードにそれを置くための任意のアイデア?ご覧のとおり、私はすでに 'output'を参照しようとしましたが、ローカル変数 –