Flutter: Highlight Searched Text

+ JavaScript: return searched portion of text

view

Node.js / JavaScript — Search

const sentence = (text, searchText) => {
const searchRegExp = new RegExp(searchText, "gi");
const index = searchRegExp.exec(text).index;
const lastPeriod = text.substring(0, index).lastIndexOf(".") + 1;

let nextPeriod = text.indexOf(".", index + searchText.length) + 1;
if (nextPeriod == 0) nextPeriod = text.length;
let searchSentence = text.substring(lastPeriod, nextPeriod).trim();

if (searchSentence.length < 250) {
nextPeriod = text.indexOf(".", lastPeriod + searchSentence.length) + 1;
if (nextPeriod == 0) nextPeriod = text.length;
searchSentence = text.substring(lastPeriod, nextPeriod).trim();
}
if (nextPeriod < text.length) searchSentence += " ...";
return searchSentence;

}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store