# 某些语言中搜索结果未高亮显示

**URL:** <https://meta.discourse.org/t/search-results-are-not-highlighted-in-some-languages/142885>\
**Category:** Support\
**Created:** [2020年二月28日 13:25 UTC](https://meta.discourse.org/t/search-results-are-not-highlighted-in-some-languages/142885 "2020-02-28T13:25:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![smith](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/smith/32/183047_2.png) [@smith](https://meta.discourse.org/u/smith)\
**Post date:** [2020年二月28日 13:25 UTC](https://meta.discourse.org/t/search-results-are-not-highlighted-in-some-languages/142885/1 "2020-02-28T13:25:08Z")

</div>

你好！在我的 Discourse 2.5.0.beta1 安装中，如果搜索语言不是英语，搜索结果中搜索关键词的高亮功能无法正常工作。这是一个 bug 还是特性？谢谢！

 ![Screenshot_20200228_162254](https://global.discourse-cdn.com/meta/original/3X/c/a/ca94e87b2a23ad7a02bab4504a81a96c32625065.png)

---

<div class="post-metadata">

**Author:** ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)\
**Post date:** [2020年二月29日 20:27 UTC](https://meta.discourse.org/t/search-results-are-not-highlighted-in-some-languages/142885/2 "2020-02-29T20:27:16Z")

</div>

这里期待什么 @sam？

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [2020年二月29日 22:06 UTC](https://meta.discourse.org/t/search-results-are-not-highlighted-in-some-languages/142885/3 "2020-02-29T22:06:06Z")

</div>

我们仅高亮完整单词。这看起来像是一个不完整的单词。

---

<div class="post-metadata">

**Author:** ![smith](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/smith/32/183047_2.png) [@smith](https://meta.discourse.org/u/smith)\
**Post date:** [2020年三月1日 13:05 UTC](https://meta.discourse.org/t/search-results-are-not-highlighted-in-some-languages/142885/4 "2020-03-01T13:05:21Z")

</div>

你好！期望找到的关键词能像这样高亮显示：

 ![Screenshot_20200301_160048](https://global.discourse-cdn.com/meta/original/3X/1/a/1aedb55129ea831aea2742f870e9016c41682edc.jpeg)

---

<div class="post-metadata">

**Author:** ![smith](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/smith/32/183047_2.png) [@smith](https://meta.discourse.org/u/smith)\
**Post date:** [2020年三月1日 13:07 UTC](https://meta.discourse.org/t/search-results-are-not-highlighted-in-some-languages/142885/5 "2020-03-01T13:07:29Z")

</div>

抱歉，此情况不适用：

 ![Screenshot_20200301_160600](https://global.discourse-cdn.com/meta/original/3X/a/3/a3c28a02a40d7236f678c20d1c0fd2c409ac028b.jpeg)

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [2020年三月2日 06:15 UTC](https://meta.discourse.org/t/search-results-are-not-highlighted-in-some-languages/142885/6 "2020-03-02T06:15:05Z")

</div>

@vinothkannans 能否快速查看一下：

> <https://github.com/discourse/discourse/blob/4635be10c88cb53779a12eb83daa38bf8101b799/app/assets/javascripts/discourse/lib/highlight-text.js.es6#L13-L16>

@smith 能否在这里发布一条包含确切希腊单词的帖子？（我认为这是希腊语）这可能是我们高亮 JavaScript 库中的一个错误。

---

<div class="post-metadata">

**Author:** ![smith](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/smith/32/183047_2.png) [@smith](https://meta.discourse.org/u/smith)\
**Post date:** [2020年三月2日 12:29 UTC](https://meta.discourse.org/t/search-results-are-not-highlighted-in-some-languages/142885/8 "2020-03-02T12:29:13Z")

</div>

这是西里尔字母，不过它源自希腊字母。以下是一些示例：

因为圣灵和我们定意不将别的重担放在你们身上，惟有几件事是不可少的： **就是** 禁戒祭偶像的物和血，并勒死的牲畜和奸淫。这几件你们若能自己禁戒不犯，那就好了。愿你们平安。

---

<div class="post-metadata">

**Author:** ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)\
**Post date:** [2020年三月6日 04:44 UTC](https://meta.discourse.org/t/search-results-are-not-highlighted-in-some-languages/142885/9 "2020-03-06T04:44:51Z")

</div>

> <https://github.com/discourse/discourse/blob/7b194743d7e10a425086d52c8e8ddd42ce1856f3/vendor/assets/javascripts/highlight.js#L100-L102>

问题出在 jQuery Highlight 插件中。该插件使用单词边界 `\b` 正则表达式来高亮单词，但该表达式不支持 Unicode 字符。

[https://medium.com/@shiba1014/regex-word-boundaries-with-unicode-207794f6e7ed](https://medium.com/@shiba1014/regex-word-boundaries-with-unicode-207794f6e7ed)

> `(?<=[\s,.:;"']|^)UNICODE_WORD(?=[\s,.:;"']|$)`

这看起来是一个可行的解决方案：思考中

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [2020年三月6日 06:00 UTC](https://meta.discourse.org/t/search-results-are-not-highlighted-in-some-languages/142885/10 "2020-03-06T06:00:10Z")

</div>

我认为 @gerhard 已经处理过这个关于 Unicode 用户名的特定问题（至少我模糊地记得）。

非常高兴看到这里有修复方案，但我们必须格外小心，确保所选的正则表达式在某些文本很长或特别复杂的情况下不会出现性能极差的情况。另外，我认为在中文里，我们甚至不需要考虑词边界？

---

<div class="post-metadata">

**Author:** ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)\
**Post date:** [2020年三月10日 18:37 UTC](https://meta.discourse.org/t/search-results-are-not-highlighted-in-some-languages/142885/11 "2020-03-10T18:37:13Z")

</div>

已创建包含修复的新 PR

> <https://github.com/discourse/discourse/pull/9163>
>
> Removed the jQuery Highlight plugin and created a new es6 highlighting library.

---

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [2020年五月15日 13:37 UTC](https://meta.discourse.org/t/search-results-are-not-highlighted-in-some-languages/142885/12 "2020-05-15T13:37:03Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
