Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

510
Views
How to use match() method when I have a variable dynamically with case insensitive?

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.

As I have tried like the following image

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!