Home Programs Compare Two Strings using RegEx in JavaScript

Compare Two Strings using RegEx in JavaScript

In this example we will check that two strings match

You will see that the pattern below has the following gi, this means

Flags Description
g Performs a global match (find all matches)
i Performs case-insensitive matching

Example 1

// string comparison
const string1 = 'maxJavascript Programs';
const string2 = 'maxjavascript programs';

// create regex
const pattern = new RegExp(string1, "gi");

// compare the stings
const result = pattern.test(string2)

if(result) 
{
    console.log('The strings are the same.');
} 
else 
{
    console.log('The strings are not the same.');
}

When you run this you will see something like this

The strings are similar.

You may also like