How to perform a case-insensitive search in TypeScript

2 Answers

0 votes
function containsIgnoreCase(str: string, toFind: string): boolean {
    return str.toLowerCase().indexOf(toFind.toLowerCase()) >= 0;
}
 
const str: string = "The FOX Profession is TypeScript Programmer";
const toFind: string = "fox";
 
const contains: boolean = containsIgnoreCase(str, toFind);
 
console.log(contains);
 
 
    
/*
run:
    
true 
   
*/

 



answered Feb 24, 2025 by avibootz
0 votes
function containsIgnoreCase(str: string, toFind: string): boolean {
    const regex = new RegExp(toFind, "i");
     
    return regex.test(str);
}
 
const str: string = "The FOX Profession is TypeScript Programmer";
const toFind: string = "fox";
 
const contains: boolean = containsIgnoreCase(str, toFind);
 
console.log(contains);
 
 
    
/*
run:
    
true 
   
*/

 



answered Feb 24, 2025 by avibootz

Related questions

1 answer 84 views
1 answer 99 views
1 answer 99 views
1 answer 102 views
1 answer 161 views
1 answer 77 views
...