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 159 views
1 answer 187 views
1 answer 181 views
1 answer 187 views
1 answer 270 views
1 answer 177 views
2 answers 243 views
...