나는 웹 사이트에 업로드 된 텍스트 파일에서 키워드를 읽고 검색하는 스크립트 작업을하고 있습니다. 문자열을 읽고 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>
업데이트
여기에서 파일 리더를위한 스크립트를 가지고 . ID에 연결하고 .innerHTML 메서드를 사용하여 업로드 된 문서에 연결해야했습니다. 루프가 올바르게 작동하는 데 새로운 문제가 생기는 것을 제외하면 모든 것이 잘 작동합니다.
사용자가이 사이트에 문서를 업로드 한 다음 문서에서 키워드를 검색하는 일련의 버튼을 클릭하게하고 싶습니다. 기후 변화에 대한 문서를 코딩하는 조직에서 일하고 있으며 프로세스 속도를 높이려고합니다. 일반적으로 사람은 문서를 읽고 컨트롤 -f을 사용하여 특정 기후 변화 키워드를 검색합니다. 키워드가 올바른 컨텍스트의 문서에있는 경우 사용자는 데이터베이스에서 키워드를 기록합니다. 이 사이트는 해당 프로세스를 자동화하고 사람들이 적절한 용어를 검색하지 않거나 모든 용어를 검색하지 않을 위험을 줄입니다.
나는 일련의 정규 표현식을 사용하여 문서에서 루프와 검색을하고 싶습니다. 루프와 표현식을 만들었지 만 배열의 각 항목을 검색 한 후 다시 루프를 시작하고 싶습니다. 예를 들어, 문서에서 키워드 을 다시 검색하면 처음부터 다시 시작하여 전체 문서에서 프로젝트을 검색하고 싶습니다. 검색에서 문서에서 파일럿을 찾은 다음 파일럿의 마지막 인스턴스를 찾으면 프로젝트으로 이동합니다. 당신은 테스트 파일을의
'function myFunction() {alert ("필요한 모든 검색어를 강조 표시 한 다음 'program'단어의 인스턴스를 찾아 검색합니다.) var count = (output.match (/ 프로그램/g) || []). 길이 경고 ("귀하의 단어 발생"+ count + "times.") –
감사합니다 NinjaM! 내 문제를 조사하고 그 기능을 벗어난 텍스트의 변수에 액세스하는 방법을 알아야한다고 생각합니다. 어떻게해야할지 모르겠다. 위의 코드에 삽입하는 방법에 대한 아이디어가 있습니까?보시다시피, 나는 이미'output'을 참조하려고 시도했으나 작동하지 않았습니다. 왜냐하면 그 지역 변수 –