I am trying to replace the below string like below:
var phone = "9871234567"
result = xxx-xxx-4567
Below is the js code I have worked upon, but this is not giving me the correct result.
var lastphdigit = phone.replace(/\d(?=(?:\D*\d){4})/g, "x");
var lastphdighyp = lastphdigit.replace(/.{3}(?!$)/g, '$&-'); //xxx-xxx-456-7
Regex is not necessary for this problem. Using
slicewould be much simpler, just get the last 4 digits