-1

I would like to have a Javascript that finds all instances that correspond in array (multi-line text file) containing the words with diacritics, that correspond the diacritic-less search query. For instance, in array of four values there are ["ir minēts", "nav mīnēts", "bija minets", "būs mints"]. By entering in the search query "minets", I expect to have the keys of the first three values — 0,1,2 — and their values with the corresponding word in bold <b>*</b> returned as the result. Thank you in advance!

ugisu
  • 21
  • 4
  • This site is not a code-writing service. Try something, do some research, and ask questions about specific difficulties you might encounter. – Pointy Sep 03 '22 at 14:31

1 Answers1

1

You could normalize the lines you have in your array, and then filter those de-accented lines for the search string:

const removeAccents = text =>
    text.normalize("NFKD").replace(/\p{Diacritic}/gu, "")

const lines = ["ir minēts", "nav mīnēts", "bija minets", "būs mints"];
// Do this just once for the array:
const plainLines = lines.map(removeAccents);

// Search a word
let search = "minets";
let plainSearch = removeAccents(search);
let lineNumbers = plainLines.map((line, i) => 
    line.includes(plainSearch) ? i : -1
).filter(lineNo => lineNo > -1);
console.log(lineNumbers);
trincot
  • 317,000
  • 35
  • 244
  • 286