Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert hyphens in javascript

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

like image 748
Thanos Avatar asked Aug 08 '11 11:08

Thanos


1 Answers

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"));
like image 173
Jason Gennaro Avatar answered Sep 21 '22 15:09

Jason Gennaro