I want to open text or word file in JavaScript. After that I want my program to scan that whether it has the word which i needed. If it has the world then it should produce an alert as word is found,else alert as word is not found. Please help me in this. I wrote an code to read from file.But I'm not sure where the content of file is stored and where to use search function.
My current code is
<h1>File reader</h1>
<div>
Select a text file:
<input type="file" id="fileInput">
<pre id="displayArea"><pre>
</div>
window.onload = function() {
var fileInput = document.getElementById('fileInput');
var displayArea= document.getElementById('fileDisplayArea');
fileInput.addEventListener('change', function(e) {
var file = fileInput.files[0];
var textType = /text.*/;
if (file.type.match(textType)) {
var reader = new FileReader();
reader.onload = function(e) {
displayArea.innerText = reader.result;
}
reader.readAsText(file);
} else {
displayArea.innerText = "File not supported!"
}
});
}
by using this I can get my file content in output,But I need output as an alert whether file has the needed word.
You are doing nothing with your result, thats why you have to store the reader.result
output in a variable, then look if the word you are looking for is inside of that output and show the alert.
Something like this...
var readResult = reader.result;
if (/Hello/.test(readResult)) {
alert("The word exist");
} else {
alert("The word does not exist");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.