如何在可编辑的 div 中突出显示特定文本?

huangapple go评论91阅读模式
英文:

How to highlight a certain text on the contenteditable div?

问题

代码中的主要目标是突出显示可编辑 div 中的特定单词。当前的方法是使用正则表达式匹配位于 { words }[ words ] 中的关键字。

  1. const regexpSquareBracket = /\[[^\]]*\]/gm;
  2. const regexCurlyBracket = /\{[^\}]*\}/gm;
  3. const squareBracketMatches = text.match(regexpSquareBracket);
  4. const curlyBracketMatches = text.match(regexCurlyBracket);

上述代码可以正确识别这些文本。

  1. Here is [the] sample [text] that matches the [certain words] in the given [text] and {highlight} the words in the text.

然而,用于突出显示文本的代码并不理想,因为它只匹配单个关键字,而不是多个关键字组合。

你需要改进以下代码的解决方案,以便也匹配多个关键字的组合。

  1. var text = `Here is [the] sample [text] that matches the [certain words] in the given [text] and {highlight} the {words in the text}.`
  2. // 找到给定列表中的字符索引
  3. const indexesOf = (arr, item) =>
  4. arr.reduce(
  5. (acc, v, i) => (v === item && acc.push(i), acc),
  6. []);
  7. const regexpSquareBracket = /\[[^\]]*\]/gm;
  8. const regexCurlyBracket = /\{[^\}]*\}/gm;
  9. const squareBracketMatches = text.match(regexpSquareBracket);
  10. const curlyBracketMatches = text.match(regexCurlyBracket);
  11. let splittedText = text.split(" ");
  12. if (squareBracketMatches) {
  13. squareBracketMatches.forEach(function(match) {
  14. var positions = indexesOf(splittedText, match)
  15. positions.forEach(function(pos) {
  16. splittedText[pos] = `<span class="highlight-bracket">${splittedText[pos]}</span>`
  17. })
  18. })
  19. }
  20. if (curlyBracketMatches) {
  21. curlyBracketMatches.forEach(function(match) {
  22. var positions = indexesOf(splittedText, match)
  23. positions.forEach(function(pos) {
  24. splittedText[pos] = `<span class="highlight-curly">${splittedText[pos]}</span>`
  25. })
  26. })
  27. }
  28. console.log(splittedText.join(" "))

请注意,这里的代码示例是为了突出显示文本中的方括号和花括号中的关键字,并创建 span 元素进行突出显示。你可以根据自己的需求进行进一步的定制。

英文:

The goal is to highlight certain words on the contenteditable div. Currently my approach is that using the regex to match the keywords that are inside { words } or [ words ].

  1. const regexpSquareBracket = /\[[^\]]*\]/gm;
  2. const regexCurlyBracket = /\{[^\}]*\}/gm;
  3. const squareBracketMatches = text.match(regexpSquareBracket);
  4. const curlyBracketMatches = text.match(regexCurlyBracket);

Above code working fine that correctly identifies this text.

  1. Here is [the] sample [text] that matches the [certain words] in the given [text] and {highlight} the words in the text.

However the code I have for highlighting the text is not great because it only matches the single keyword rather than [multiple keywords compine].

I need you to give me the solution for how to improve below code that also matches the multiple keywords compine.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
var text = Here is [the] sample [text] that matches the [certain words] in the given [text] and {highlight} the {words in the text}.

// Find character index in a given list
const indexesOf = (arr, item) =>
arr.reduce(
(acc, v, i) => (v === item && acc.push(i), acc),
[]);

const regexpSquareBracket = /[[^]]]/gm;
const regexCurlyBracket = /{[^}]
}/gm;

const squareBracketMatches = text.match(regexpSquareBracket);
const curlyBracketMatches = text.match(regexCurlyBracket);

let splittedText = text.split(" ");

if (squareBracketMatches) {
squareBracketMatches.forEach(function(match) {
var positions = indexesOf(splittedText, match)
positions.forEach(function(pos) {
splittedText[pos] = &lt;span class=&quot;highlight-bracket&quot;&gt;${splittedText[pos]}&lt;/span&gt;
})
})
}

if (curlyBracketMatches) {
curlyBracketMatches.forEach(function(match) {
var positions = indexesOf(splittedText, match)
positions.forEach(function(pos) {
splittedText[pos] = &lt;span class=&quot;highlight-curly&quot;&gt;${splittedText[pos]}&lt;/span&gt;
})
})
}
console.log(splittedText.join(" "))
<!-- end snippet -->

答案1

得分: 1

I'll provide the translated code section:

  1. var text = `这是一个匹配给定文本中的特定单词并高亮文本中的单词的示例文本。`
  2. const removeDuplicates = arr => [...new Set(arr)];
  3. const regexpSquareBracket = /\[[^\]]*\]/gm;
  4. const regexCurlyBracket = /\{[^\}]*\}/gm;
  5. const squareBracketMatches = text.match(regexpSquareBracket);
  6. const curlyBracketMatches = text.match(regexCurlyBracket);
  7. for (const sq of removeDuplicates(squareBracketMatches))
  8. text = text.replaceAll(sq, `<span class="highlight-bracket">${sq}</span>`)
  9. for (const curly of removeDuplicates(curlyBracketMatches))
  10. text = text.replaceAll(curly, `<span class="highlight-curly">${curly}</span>`)
  11. console.log(text)

Note: I have translated the JavaScript code and left the code parts, such as variable names and regular expressions, as is.

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. var text = `Here is [the] sample [text] that matches the [certain words] in the given [text] and {highlight} the {words in the text}.`

const removeDuplicates = arr => [...new Set(arr)];

  1. const regexpSquareBracket = /\[[^\]]*\]/gm;
  2. const regexCurlyBracket = /\{[^\}]*\}/gm;
  3. const squareBracketMatches = text.match(regexpSquareBracket);
  4. const curlyBracketMatches = text.match(regexCurlyBracket);
  5. for (const sq of removeDuplicates(squareBracketMatches))
  6. text = text.replaceAll(sq, `&lt;span class=&quot;highlight-bracket&quot;&gt;${sq}&lt;/span&gt;`)
  7. for (const curly of removeDuplicates(curlyBracketMatches))
  8. text = text.replaceAll(curly, `&lt;span class=&quot;highlight-curly&quot;&gt;${curly}&lt;/span&gt;`)

console.log(text)

<!-- end snippet -->

If the any of the brackets can be nested, sort the matches by longer string first.

huangapple
  • 本文由 发表于 2023年4月11日 13:06:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定