在 ASP.Net 中搜索关键字突出显示
我正在输出给定关键字字符串的搜索结果列表,并且我希望突出显示搜索结果中的任何匹配关键字。每个单词都应该用跨度或类似的东西包裹起来。我正在寻找一个有效的函数来做到这一点。
例如
,关键字:“lorem ipsum”
结果:“包含 lorem 和 ipsum 的某些文本”
所需的 HTML 输出:“包含 lorem 和 ipsum
"
我的结果不区分大小写。
I am outputting a list of search results for a given string of keywords, and I want any matching keywords in my search results to be highlighted. Each word should be wrapped in a span or similar. I am looking for an efficient function to do this.
E.g.
Keywords: "lorem ipsum"
Result: "Some text containing lorem and ipsum"
Desired HTML output: "Some text containing <span class="hit">lorem</span> and <span class="hit">ipsum</span>
"
My results are case insensitive.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这就是我的决定。我可以在我的页面/页面部分中的相关字符串上调用扩展函数:
还有其他建议或评论吗?
Here's what I've decided on. An extension function that I can call on the relevant strings within my page / section of my page:
Any further suggestions or comments?
尝试使用 Lucene.net
http: //incubator.apache.org/lucene.net/docs/2.0/Highlighter.Net/Lucene.Net.Highlight.html
使用方法:
http://davidpodhola.blogspot.com/2008/02/how-to-highlight-phrase-on -results-from.html
编辑:
只要 Lucene.net 荧光笔不适合这里新链接:
http://mhinze .com/archive/search-term-highlighter-httpmodule/
try highlighter from Lucene.net
http://incubator.apache.org/lucene.net/docs/2.0/Highlighter.Net/Lucene.Net.Highlight.html
How to use:
http://davidpodhola.blogspot.com/2008/02/how-to-highlight-phrase-on-results-from.html
EDIT:
As long as Lucene.net highlighter is not suitable here new link:
http://mhinze.com/archive/search-term-highlighter-httpmodule/
使用 jquery 高亮插件。
为了在服务器端突出显示它,
您可以使用正则表达式进行高级文本替换。
您还可以将上述代码编写在 HttpModule 中,以便可以在其他应用程序中重复使用。
Use the jquery highlight plugin.
For highlighting it at server side
You can use regular expressions for advanced text replacing.
You can also write the above code in an HttpModule so that it can be re used in other applications.
上面答案的延伸。 (没有足够的声誉来发表评论)
为了避免当搜索条件为 [span pan an a] 时 span 被替换,找到的单词被替换为其他内容而不是替换回来...虽然不是很有效...
An extension to the answer above. (don't have enough reputation to give comment)
To avoid span from being replaced when search criteria were [span pan an a], the found word was replaced to something else than replace back... not very efficient though...