How can I find an array variable case-insensitively?

  • 0
    I have an array (arr) with values ​​and a variable (find) by which the values ​​of the array are searched.
    I tried to make it so that the user entered a variable and the array variables were found by it, but the condition is not met if I enter a piece of the same variable.
    Here is the code
    var arr = ['Tom Riddle', 'Ron Weasley', 'Harry Potter', 'Hermione Granger'];
    var find = prompt('Введите значение', '0');
    function findIt() {
      var del;
      if (!del) { del = '_//_'; }
      arr = arr.join(del).toLowerCase().split(del);
      return arr.indexOf(find.toLowerCase());
    }
    var index = findIt(arr, find);
    if (~index) {
      alert('Есть ' + arr[index]);
    } else {
      alert('Ничего нету...');
    }
    JavaScript Anonymous, Mar 19, 2019

  • 1 Answers
  • 0
    const escapeRegExp = (str) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
    const findRegExp = new RegExp(escapeRegExp(find), 'i');
    const index = arr.findIndex((name) => findRegExp.test(name));
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!