React js highlight matching text
React js highlight matching text:In React.js, you can highlight matching text by using the String.prototype.replace()
method in combination with regular expressions and JSX. First, you can split the text into an array of words. Then, using map()
, iterate over each word and replace the matching portion with a <span>
element containing a CSS class for highlighting. Finally, concatenate the modified words and render the result in the JSX. By applying appropriate CSS styles to the highlighted class, you can achieve the desired effect of highlighting matching text.




Thanks for your feedback!
Your contributions will help us to improve service.
How can React.js be used to highlight matching text in a user interface?
This React.js code demonstrates how to highlight matching text. The code sets up a React component called App
that contains a text container, an input field for searching, and a function for highlighting text.
When the user enters a search term, the handleSearchTermChange
function updates the state with the entered value and triggers the highlightText
function. The highlightText
function uses the replace
method with a regular expression to find matches for the search term in the text. It replaces the matches with a <span>
element wrapped around the matched text, applying a specified CSS class for highlighting.
The resulting JSX is rendered, and the matching text is visually highlighted using the specified CSS class.
React js highlight matching text Example
<script type="text/babel">
const { useState } = React;
const App = () => {
const [text, setText] = useState("To highlight matching text in React.js, you can use the String.prototype.replace() method in combination with regular expressions and JSX. Create a function that takes the input text and search query as parameters. Use the replace() method with a regular expression and the query as the pattern, wrapping it in a span element with a CSS class for highlighting. Return the modified JSX with the matching text highlighted. Then, call this function within your component, passing the relevant values. Finally, render the resulting JSX, and the matching text will be visually highlighted using the specified CSS class.");
const [searchTerm, setSearchTerm] = useState("");
const [highlightColor, setHighlightColor] = useState("#FFFF00");
const highlightText = () => {
const textContainer = document.querySelector(".text-container");
const highlightedText = textContainer.querySelector(".highlight");
if (highlightedText) {
highlightedText.outerHTML = highlightedText.innerHTML;
}
const regex = new RegExp(searchTerm, "gi");
textContainer.innerHTML = text.replace(regex, match => {
return `<span class="highlight" style="background-color: ${highlightColor};">${match}</span>`;
});
};
const handleSearchTermChange = (e) => {
setSearchTerm(e.target.value);
highlightText();
};
const highlightedText = () => {
const regex = new RegExp(searchTerm, "gi");
return text.replace(regex, match => {
return `<span style="background-color: ${highlightColor};">${match}</span>`;
});
};
return (
<div className='container'>
<h3>React Js Highlight Matching Text</h3>
<label htmlFor="search">Search:</label>
<input
id="search"
value={searchTerm}
onChange={handleSearchTermChange}
onKeyUp={highlightText}
/>
<div className="text-container">
<p dangerouslySetInnerHTML={{ __html: highlightedText() }}></p>
</div>
</div>
);
}
ReactDOM.render(<App />, document.getElementById('app'));
</script>