0

I'm trying to do a function which returns all matches in a given array of strings from an input using RegEx. The code is the following:

function checkWord(input, myArray) {
    let reg = new RegExp(input.split('').join('\\w*\\s*\\w*').replace(/\W/, ""), 'i');

    return myArray.filter(function (f) {
        if (f.match(reg)) {
            return f;
        } 
   }); 
}


$input.on("keyup", function () {
    let result = checkWord($input.val(), arr);
    $divResult.html(result); 
});

I have this implemented on the "keyup" event from an input, it returns all matches but doesn't work with accented words, how can I check and match accented words inside the array of strings? I mean, if the array contains accented words, it won't match non-accented words from the input.

I'm testing it with this array:

let arr = [ "Álvaro", "Lucía", "Ramón", "á", "é", "í", "ó", "ú", "Alvaro", "Lucia", "David", "Joaquín", "Pepe", "Paco", "Barça", "äe", "ë", "ï", "ö", "ü", "à", "è", "ì", "ò", "ù" ];

I've tried all RegEx suggested on comments but I can't get this to work :(

Thanks.

David AK
  • 125
  • 1
  • 14
  • Looks like `checkWord` request 2 parameters, but you only send one `checkWord($input.val())` – Carsten Løvbo Andersen May 24 '18 at 11:17
  • Are you sure this code works? Your `checkWord` function requires 2 arguments and it does not check for missing 2nd argument, but you call it with a single argument. Re matching accents, `\w` does not seem to include these, try `[A-Za-zÀ-ÿ]` instead (or even better, read this question: https://stackoverflow.com/questions/20690499/concrete-javascript-regex-for-accented-characters-diacritics). – alx May 24 '18 at 11:20
  • Ye sorry forgot to add it here to simplify the code, In my code the array is global, the code works, sorry about that, I just edited the post. – David AK May 24 '18 at 11:22
  • In Chrome and ECMAScript 2018 compatible JS engines, you may use `new RegExp(input.split('').join('[\\p{L}\\p{N}_]*\\s*[\\p{L}\\p{N}_]*').replace(/[^\p{L}\p{N}_]+/gu, ""), 'iu')`. Else, you need to use `XRegExp` and use `[\\pL\\pN_]` like patterns. – Wiktor Stribiżew May 25 '18 at 21:43

1 Answers1

0

Replace the RegExp by:

let reg = new RegExp(input.split('').join('[A-Za-zÀ-ÿ]*\\s*[A-Za-zÀ-ÿ]*').replace(/[^A-Za-zÀ-ÿ]/, ""), 'i');

While \w allowes all alphabetic characters(meaning [a-zA-Z]), [A-Za-zÀ-ÿ] also allowes accented characters. And \W means all BUT alphabetic characters([^a-zA-Z]) and [^A-Za-zÀ-ÿ] also doesn't allow accented characters.

Kokogino
  • 984
  • 1
  • 5
  • 17