Below is my code I used to Highlight Search values in my list. I used a function getHighlightedText
and passed this function to my list.
getHighlightedText = text => {
// search value stored in this.state.value
const { value } = this.props;
const space = new RegExp('\\b\\B\\W+\\B\\b]', 'g');
// value is split
const findWords = value.split(space).map(fw => fw.toLowerCase());
console.log('split the value:', findWords)
// text is split
const textWords = text.split(space);
console.log('split the search:', textWords)
// each word in the array is mapped
const output = textWords.map(word => {
const lower = word.toLowerCase();
// logic to check search value includes word
if (findWords.includes(lower)) {
return <Text style = {{backgroundColor: 'coral'}}>{word}</Text>
} else {
return <Text>{word}</Text>
}
});
return <Text>{output}</Text>
}
// using my getHighlightedText in my renderItem
return <Text>{getHighlightedText(Desc)}</Text>;
This logic works fine only for complete strings and not for partial strings. In the below image, "json" and "url" are highlighted correctly. But as I typed "Noti" and not "Notification" this string is not highlighted.
How can I achieve this?? any ideas pleaseeee