Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlight text using ReactJS

I'm trying to highlight text matching the query but I can't figure out how to get the tags to display as HTML instead of text.

var Component = React.createClass({     _highlightQuery: function(name, query) {         var regex = new RegExp("(" + query + ")", "gi");         return name.replace(regex, "<strong>$1</strong>");     },     render: function() {         var name = "Javascript";         var query = "java"         return (             <div>                 <input type="checkbox" /> {this._highlightQuery(name, query)}             </div>         );     } }); 

Current Output: <strong>Java</strong>script

Desired Output: Javascript

like image 996
Andrew Hunt Avatar asked Apr 15 '15 14:04

Andrew Hunt


People also ask

How do you highlight text in react JS?

To highlight text using React, we can create our own component where we check whether each word matches what we want to highlight. to create the Highlighted component to highlight the text set as the highlight option.

How do you highlight text in JavaScript?

Highlight Text Using the Mark Tag Method in JavaScriptIf you surround any text inside the mark tag, the browser will automatically highlight it in a striking yellow color. This will make highlighting a searched text quite a simple task then.

How do you select text in react?

To select the all text in an input field, we can use the e. target. select() method in react. Here is an example of a functional react component, that selects the text by clicking on the input field.

How do you highlight text in HTML CSS?

How Do I Highlight Text In CSS? To Highlight text in HTML you have to use an inline element such as the <span> element and apply a specific background style on it. This will create the highlighting effect, which you can tweak in many different ways to create different looks.


2 Answers

Here is my simple twoliner helper method:

getHighlightedText(text, highlight) {     // Split text on highlight term, include term itself into parts, ignore case     const parts = text.split(new RegExp(`(${highlight})`, 'gi'));     return <span>{parts.map(part => part.toLowerCase() === highlight.toLowerCase() ? <b>{part}</b> : part)}</span>; } 

It returns a span, where the requested parts are highlighted with <b> </b> tags. This can be simply modified to use another tag if needed.

UPDATE: To avoid unique key missing warning, here is a solution based on spans and setting fontWeight style for matching parts:

getHighlightedText(text, highlight) {     // Split on highlight term and include term into parts, ignore case     const parts = text.split(new RegExp(`(${highlight})`, 'gi'));     return <span> { parts.map((part, i) =>          <span key={i} style={part.toLowerCase() === highlight.toLowerCase() ? { fontWeight: 'bold' } : {} }>             { part }         </span>)     } </span>; } 
like image 97
peter.bartos Avatar answered Sep 23 '22 21:09

peter.bartos


Here is an example of a react component that uses the standard <mark> tag to highlight a text:

const Highlighted = ({text = '', highlight = ''}) => {    if (!highlight.trim()) {      return <span>{text}</span>    }    const regex = new RegExp(`(${_.escapeRegExp(highlight)})`, 'gi')    const parts = text.split(regex)    return (      <span>         {parts.filter(part => part).map((part, i) => (             regex.test(part) ? <mark key={i}>{part}</mark> : <span key={i}>{part}</span>         ))}     </span>    ) } 

And here is how to use it

<Highlighted text="the quick brown fox jumps over the lazy dog" highlight="fox"/> 
like image 43
Henok T Avatar answered Sep 23 '22 21:09

Henok T