javascript - 检查数据库中“单词”集合中的所有单词,并检查文本中的任何单词是否与任何单词匹配
问题描述
我在数据库 MongoDB 中有一个名为 words 的集合,它存储所有单词。它们已通过后端查询提取并推送到前端。
这是在前端完成的:
this.annotationSub = this.annotationService
.getWordUpdateListener()
.subscribe((thewords: ComplexWord[]) => {
this.thewords = thewords;
this.thewords.map(word => {
if (word.word === this.setWord) {
this.wordIWant = word.word;
}
console.log(word);
});
最console.log(word);
上面给出这些字段=
{word: "Lorem", annotation: "Explain Lorem"},
{word: "Aenean", annotation: "Explain Aenean"},
{word: "Hello", annotation: "Explaining Hello"}
这将检索所有文本:
this.postsService.getPosts();
this.postsSub = this.postsService
.getPostUpdateListener()
.subscribe((posts: Post[]) => {
this.posts = posts;
this.posts.map(post => {
if (post.id === this.id) {
this.postIWant = post.fileText;
}
});
});
我this.postIWant
已经从帖子中得到了所有的文字。
现在如何检查是否有任何单词与中的文本匹配this.postIWant
?
提前谢谢了
解决方案
这是最好的解决方案。
将文本传递给函数:
function complexWordIdentification(text) {
const complexWords = ['Hello', 'World', 'Complex Phrase'];
const results = [];
let match, regexp, result;
for (let i = 0; i < complexWords.length; i++) {
// the complex word we are checking in this iteration
const complexWord = complexWords[i];
regexp = new RegExp(complexWord, 'g');
while ((match = regexp.exec(text)) !== null) {
result = {
begin: (regexp.lastIndex - complexWords[i].length),
end: regexp.lastIndex,
text: complexWord
};
results.push(result);
}
}
return results;
}
推荐阅读
- python - 如何创建允许用户根据列(python)中的值过滤树视图的按钮或选项?
- wpf - 如何将多个着色器效果应用于 DrawingVisual?
- php - Symfony docker 配置中的两个服务
- python - 从一个图中的坐标列表中绘制多条线
- javascript - Vue Js在select2中没有得到select选项值
- javascript - Redsys 问题(MVC .net 项目) - 不显示付款消息
- angular - LoginPage 是 2 个模块声明的一部分
- azure - Azure 日志分析休息 API 调用错误 403
- android - Android-如何在本地存储和检索 SecretKey 对象并检索它
- javascript - Javascript 阻止或减慢浏览器的速度。如何防止警报或加速脚本