使用 JavaScript 代码从英文小说中查找摘录文本/n/n要实现通过摘录文本找到小说原文,可以使用字符串匹配的方法。以下是一段简单的 JavaScript 代码实现:/n/njavascript/n// 原文小说文本/nconst novelText = 'Once upon a time, there was a beautiful princess named Cinderella. She lived with her stepmother and stepsisters in a small cottage.';/n/n// 摘录的文本/nconst excerpt = 'beautiful princess named Cinderella. She lived with her stepmother';/n/n// 删除摘录文本中的标点符号和空格/nconst cleanedExcerpt = excerpt.replace(/[^/w/s]/gi, '').trim();/n/n// 使用正则表达式进行匹配查找/nconst regex = new RegExp(cleanedExcerpt, 'i');/nconst match = novelText.match(regex);/n/n// 输出匹配结果/nif (match) {/n console.log('原文中找到匹配的文本:', match[0]);/n} else {/n console.log('未找到匹配的文本');/n}/n/n/n在上述代码中,我们首先定义了原文小说文本 novelText 和摘录的文本 excerpt。然后,我们通过删除摘录文本中的标点符号和空格,得到了清理后的文本 cleanedExcerpt。接下来,我们使用正则表达式来进行匹配查找,不区分大小写,如果找到匹配的文本,则输出匹配结果;否则,输出未找到匹配的文本。


原文地址: https://www.cveoy.top/t/topic/qtbF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录