简体   繁体   English

突出显示文本中特定单词的出现

[英]Highlight specific occurrence of word in text

I am trying to highlight a specific occurrence of a word in a text using jquery/js.我正在尝试使用 jquery/js 突出显示文本中特定出现的单词。 Trying to find out if there are any existing libraries I can use.试图找出是否有任何我可以使用的现有库。 I read about mark.js but it does not offer the functionality I need.我阅读了有关 mark.js 的信息,但它没有提供我需要的功能。 Example Text: "In a home, there is a room, the room has a door"示例文本:“在家里,有一个房间,房间有门”
Highlight word: "room"突出词:“房间”
Occurrence: 2 The second "room" in the text needs to be highlights.出现次数:2 文中第二个“房间”需要高亮。
Please suggest.请建议。 Thanks!谢谢!

Just pass in the specific index of the token (character sequence you are looking for) to a function that takes the string, token, and index as parameters.只需将令牌的特定索引(您要查找的字符序列)传递给将字符串、令牌和索引作为参数的 function。 You can now use the 2nd parameter of indexOf to update the beginning of where the string will be searched from using the last result:您现在可以使用 indexOf 的第二个参数来更新将使用最后一个结果搜索字符串的开始位置:

 const highlighter = (string, token, index) => { let n = -1 for (let i = 0; i <= index; i++) { n = string.indexOf(token, n + 1) } return string.slice(0, n) + string.slice(n).replace(token, '<span class="highlight">' + token + '</span>') } const text = 'In a home, there is a room, the room has a door.<br>' const firstRoom = highlighter(text, 'room', 0) const secondRoom = highlighter(text, 'room', 1) $('#result').append(firstRoom) $('#result').append(secondRoom)
 .highlight { background-color: lightblue; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="result"></div>

The -1 is important since this function would otherwise miss the first token occurence if it appears at the start of the string. -1 很重要,因为如果此 function 出现在字符串的开头,它将错过第一个令牌出现。

 // To replace all (commented out so it doesn't interfere with the other one) /* var p = document.getElementById("text"); // Gets the <p> var text = p.innerText; // Gets the text it contains var wordToReplace = "room"; // Which word should be replaced var newtext = text.replaceAll(wordToReplace, '<span class="highlight">' + wordToReplace + '</span>'); // Replaces the word with the word wrapped in <span> tags, so it will look highlighted p.innerHTML = newtext; // Change it back */ // To replace specific occurences var paragraph = document.getElementById("text"); // Gets the <p> var txt = paragraph.innerText; // Gets the text it contains var textToReplace = 'room' // Word to be replaced var replace = RegExp(textToReplace, 'g'); var matches = txt.matchAll(replace); // Gets all places where the text matches the word var replacementPositions = [0, 2]; // Occurences which should be highlighted var i = 0; // Which match this is; starts at 0 for (const match of matches) { // For each match... var text = match[0]; // The matching text var start = match.index; // Start position var end = match.index + text.length; // End position if (replacementPositions.includes(i)) { // If it should be replaced (in the replacementPositions array) var startText = txt.substring(0, start - 1); // Text before match var endText = txt.substring(start); // Text after match endText = endText.substring(text.length); // Removes matching text from the text after the match txt = startText + '<span class="highlight">' + text + '</span>' + endText; // Insert the match back in, wrapped in a <span> } i++; // Increment } paragraph.innerHTML = txt; // Set the paragraph text back
 .highlight { background-color: yellow; }
 <p id="text">First: room. Another one: room. Last time: room</p>

The first method detects all occurrences of the word and wraps them in a <span> .第一种方法检测单词的所有出现并将它们包装在<span>中。 The <span> has a style that sets the background color of the text, so it looks 'highlighted'. <span>具有设置文本背景颜色的样式,因此它看起来“突出显示”。

The second method goes through every occurrence of the word and checks if it should be highlighted.第二种方法检查单词的每一次出现并检查它是否应该被突出显示。 If so, it does some operations that wrap that occurrence in a <span> , like the method above.如果是这样,它会执行一些操作,将该事件包装在<span>中,就像上面的方法一样。

You could do it like this:你可以这样做:

 let text = "In a home, there is a room, the room has a door."; let search = "room"; var n = text.lastIndexOf(search); text = text.slice(0, n) + text.slice(n).replace(search, "<span class='highlight'>" + search + "</span>"); $("#result").append(text);
 .highlight { color:red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="result"> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM