我在搜索长文本中的字符串时遇到了一些问题.我只想提取搜索的文本和粗体搜索的文本可能有10-20个字符之前和之后的搜索字符.
所以基本上我想要实现的是,例如.从那篇文章中可以看出:
你好,我想实现一个新的目标,为其创建一个良好的搜索栏 我的应用程序.它应该和任何其他搜索栏一样.
因此,如果我想搜索"Good Search",它应该返回如下内容:
...实现一个新目标,为我的应用程序创建good search个栏.它 应该只是...
做这件事最好的办法是什么?我try 了一些类似的方法:
const text = "Hi there, I want to achieve a new goal to create a good search bar for my app. It should be just as any other search bar."
const search_text = "good search"
const radius = 10;
// To determine where is the first character
const find_first = text.search(search_text)
const search_from = find_first - radius;
// To ensure that we are taking from first with length of searched text and additional ones
const search_to = find_first + search_text.length + radius
但是,这仅仅是为了确定如何判断要获取哪些字符.但如何列出它们并突出显示呢?