从文本中提取包含搜索关键词的完整句子,对于重复的句子只保留最长的一个。
| 参数 |
说明 |
类型 |
Array |
包含关键词的句子数组(已去重) |
string[] |
| 参数 |
说明 |
类型 |
默认值 |
text |
原文本 |
string |
无 |
searchValue |
搜索关键词 |
string |
无 |
import { getMatchingSentences } from 'ranuts';
const text = '这是第一句话。这是包含关键词的第二句话。这是第三句话。';
const sentences = getMatchingSentences(text, '关键词');
console.log(sentences); // ['这是包含关键词的第二句话。']
import { getMatchingSentences } from 'ranuts';
const text = '第一句包含关键词。第二句也包含关键词。第三句没有。';
const sentences = getMatchingSentences(text, '关键词');
console.log(sentences); // ['第一句包含关键词。', '第二句也包含关键词。']
import { getMatchingSentences } from 'ranuts';
const text = '短句关键词。这是一个包含关键词的长句子。';
const sentences = getMatchingSentences(text, '关键词');
// 只保留最长的句子
console.log(sentences); // ['这是一个包含关键词的长句子。']
import { getMatchingSentences } from 'ranuts';
console.log(getMatchingSentences('', '关键词')); // []
console.log(getMatchingSentences('文本', '')); // []
- 句子识别:通过句号(。)、点号(.)、换行符(\n)、感叹号(!)、问号(?、?)识别句子边界。
- 去重处理:对于重叠的句子,只保留最长的一个。
- 大小写不敏感:搜索时忽略大小写。
- 用途:常用于搜索高亮、文本摘要、搜索结果展示等场景。