Confirm the Ending - new RegExp() method

I am trying to work out how to use the $ at the end of a regex that is passed to the new RegExp() method from a function argument. My code:

function confirmEnding(str, target) {
  let r = new RegExp(target, 'i'); // need to make regex only identify pattern at the end
  if (r.test(str) == true) {
    return true;
  } else {
    return false;
  }
}

Any help would be appreciated.
Thanks

Thanks for your help. I tried this in both node js and vanilla js and without the else statement I get undefined.

Apologies, I just realised what you meant. This works. My refactored code:

function confirmEnding(str, target) {
  var funcTarget = target + '$';
  let r = new RegExp(funcTarget, 'i');
  return r.test(str);
}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.