What is the easiest way to insert hyphens in javascript?
I have a phone number eg.1234567890
while displaying in the front end, I have to display it as 123-456-7890 using javascript.
what is the simplest and the quickest way to achieve this?
Thanks
Quickest way would be with some regex:
Where n
is the number
n.replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3");
Example: http://jsfiddle.net/jasongennaro/yXD7g/
var n = "1234567899"; console.log(n.replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3"));
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