Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
27
rated 0 times [  33] [ 6]  / answers: 1 / hits: 51377  / 13 Years ago, mon, august 8, 2011, 12:00:00

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 way to achieve this?


More From » insert

 Answers
17

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));




[#90745] Saturday, August 6, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ibrahimr

Total Points: 468
Total Questions: 99
Total Answers: 93

Location: Serbia
Member since Sun, Jul 11, 2021
3 Years ago
ibrahimr questions
Tue, Jun 22, 21, 00:00, 3 Years ago
Tue, May 26, 20, 00:00, 4 Years ago
Fri, Jan 3, 20, 00:00, 4 Years ago
;