How to perform a case-insensitive search in JavaScript

2 Answers

0 votes
function containsIgnoreCase(str, toFind) {
    return str.toLowerCase().indexOf(toFind.toLowerCase()) >= 0;
}

const str = "The FOX Profession is JavaScript Programmer";
const toFind = "fox";

const contains = containsIgnoreCase(str, toFind);

console.log(contains);



   
/*
run:
   
true
  
*/


 



answered Feb 24, 2025 by avibootz
0 votes
function containsIgnoreCase(str, toFind) {
    const regex = new RegExp(toFind, "i");
    
    return regex.test(str);
}

const str = "The FOX Profession is JavaScript Programmer";
const toFind = "fox";

console.log(containsIgnoreCase(str, toFind));


   
/*
run:
   
true
  
*/

 



answered Feb 24, 2025 by avibootz

Related questions

1 answer 155 views
1 answer 183 views
1 answer 176 views
1 answer 183 views
1 answer 263 views
1 answer 170 views
2 answers 238 views
...