I am building a naive rstrip
function. And it works properly like this when I hardcode the actual characters:
const rstrip = (s, chars) => s.replace(/[r!]+$/g, "");
console.log(rstrip("Hello!!!!r", '!r'))
However, if I try and use the chars
variable, I have trouble escaping it:
const rstrip = (s, chars) => s.replace(/[chars]+$/g, "");
console.log(rstrip("Hello!!!!r", '!r'))
What would be the proper way to 'escape' the variable so that the value !r
replaces the chars
variable in the replace
method?