javascript - 有没有办法将文本文档写入屏幕并在文档中搜索特定单词并打印出这些单词
问题描述
document.getElementById('inputfile').addEventListener('change', function() {
var fr = new FileReader();
fr.onload = function() {
document.getElementById('output').textContent = fr.result;
}
fr.readAsText(this.files[0]);
})
<input type="file" name="inputfile" id="inputfile">
<br>
<pre id="output"></pre>
这是我现有的将文档内容显示到屏幕上的代码
解决方案
是fr.result
一个字符串。您可以使用 将此字符串拆分为单词数组String.split(' ')
。并检查这些单词中的任何一个是否与您的预定义单词匹配,这些单词也应该在数组中使用Array.some(checkingFunction)
. 这是您的代码的延续
let myWords = ['hello', 'world', 'what', 'is', 'happening'];
function checkIfWordExistInmyWords(word){
return myWords.includes(word);
}
let docWords = fr.result.split(' ');
console.log(docWords.some(checkIfWordExistInmyWords))
推荐阅读
- javascript - 通过直接路径在树中查找路径
- google-apps-script - 通过 PropertiesService 在 Google Apps 脚本中存储和检索选择元素
- arrays - 如何将数据插入到一个空数组并依次显示在标签中的文本字段中?
- asp.net-mvc - 对模型属性的验证不会为字符串长度生成错误消息
- laravel - [Vue 警告]:渲染错误:Vue.js 中的“TypeError:this.input.reduce 不是函数”
- google-apps-script - 将行添加到过滤范围
- php - 我的代码没有从我的网站发送电子邮件
- drupal-7 - 按日期查看结果组并计算一个月的结果
- javascript - 如何从输入中获取数值?
- docusignapi - 信封中可点击的超链接文本