I have a number variable at JavaScript and i want it replaced in last 4 character. Example:
I have a number 123456789 and i want it to be replaced like this 12345****
Is there any regex to do that in JavaScript?
Use replace()
with regex /\d{4}$/
var res = '123456789'.replace(/\d{4}$/, '****');
document.write(res);
Regex explanation
substring()
or substr()
var str = '123456789',
res = str.substr(0, str.length - 4) + '****';
document.write(res);
You could use substring
as well:
var s = '123456789';
var ns = s.substring(0, s.length - 4) + '****';
document.write(ns);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With