I need an alternative of replaceAll method, as it was added in ES2021/ES12.
replaceAll
let a = "xxxx-x[email protected]"; a = a.replace("xxxx-", ""); console.log(a);
// Actual [email protected] // Required [email protected]
You can do it via regular expression: use the /g (“match globally”) modifier with a regular expression argument to replace if you are assigning a RegExp directly
/g
replace
let a = "[email protected]"; a = a.replace(/xxxx-/g, "");
if you want to pass RegExp with some variable
let a = "[email protected]"; const code = 'xxxx'; const key = new RegExp(`${code}-`,"g"); a = a.replace(key, "");
You can not update value again in const.
Thanks for notifying me.