How to useregular expression in Javascript
A regular expression (regex) is a sequence of characters that define a search pattern.
Syntax:
let regex = new RegExp(pattern, flags);
Example: In this example, we will use the regular expression to compare two strings.
let str1 = "this iS w3wiki";
let str2 = "This IS GeeksfOrgeeks";
console.log(str1)
console.log(str2)
function myGeeks() {
let pattern = new RegExp(str1, 'gi');
let result = pattern.test(str2);
console.log(result ? true : false);
}
myGeeks()
Output
this iS w3wiki This IS GeeksfOrgeeks true
This approach is similar to Approach 2 but provides an alternative method using the === operator directly with the lowercase versions of the strings. It’s concise and readable.
Compare the Case Insensitive strings in JavaScript
Comparing strings in a case-insensitive manner means comparing them without taking care of the uppercase and lowercase letters.
Here are some common approaches to compare the case-insensitive string in JavaScript:
Table of Content
- JavaScript toUpperCase() function
- JavaScript toLowerCase() function
- Using localCompare()
- Using regular expression