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

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

How to highlight a certain text on the contenteditable div?

问题

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

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

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

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

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

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

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

// 找到给定列表中的字符索引
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] = `<span class="highlight-bracket">${splittedText[pos]}</span>`
    })
  })
}

if (curlyBracketMatches) {
  curlyBracketMatches.forEach(function(match) {
    var positions = indexesOf(splittedText, match)
    positions.forEach(function(pos) {
      splittedText[pos] = `<span class="highlight-curly">${splittedText[pos]}</span>`
    })
  })
}
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 ].

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

Above code working fine that correctly identifies this text.

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:

var text = `这是一个匹配给定文本中的特定单词并高亮文本中的单词的示例文本。`

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

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

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

for (const sq of removeDuplicates(squareBracketMatches))
  text = text.replaceAll(sq, `<span class="highlight-bracket">${sq}</span>`)

for (const curly of removeDuplicates(curlyBracketMatches))
  text = text.replaceAll(curly, `<span class="highlight-curly">${curly}</span>`)

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 -->

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)];

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

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

for (const sq of removeDuplicates(squareBracketMatches)) 
  text = text.replaceAll(sq, `&lt;span class=&quot;highlight-bracket&quot;&gt;${sq}&lt;/span&gt;`)

for (const curly of removeDuplicates(curlyBracketMatches))
  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
  • javascript

如何在没有填充的情况下,从文本到

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

确定