Suppose I have case insensitive strings to be compared like,
const a = 'b'
const c = 'B'
So for comparing these, I used the match() method, but it works for static strings like we can use / for doing case insensitive comparison.
Eg. a.match(/b/), but I need to do it dynamically like
if I have a variable, test1 = 'b' and test2 = 'B', but when doing like this test1.match(/test2/) it gets "test2" as string but I need the value('B') in it.
You can use the RegExp constructor to make a dynamic regular expression that depends on the variable's value.
function isMatch(str, testCase) {
var rgx = new RegExp(testCase);
return str.match(rgx);
}
console.log(isMatch("b", "b"));
console.log(isMatch("b", "B"));
console.log(isMatch("hello world", "hello"));
console.log(isMatch("hello world", "hi"));
Check MDN for more info.