I am trying to replace 2% with ''(empty) using regexp concept. If input string is either % or 2%, it should be replaced with ''(empty):
const str = "2%";
console.log(`2%`.replace(/^\d%$|\d(?=%)/, ''));
console.log(`2%`.replace(/\d(?=%)|^\d%$/, ''));
(a|b) Matches the a or the b part of the subexpression.
"2%".replace(/^\d%$|\d(?=%)/, '')
. This works well.
But, "2%".replace(/\d(?=%)|^\d%$/, '')
does not.