在javascript中插入连字符 [英] Insert hyphens in javascript

查看:70
本文介绍了在javascript中插入连字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript中插入连字符的最简单方法是什么?

What is the easiest way to insert hyphens in javascript?

我有一个电话号码,例如:1234567890

I have a phone number eg.1234567890

在前端显示时,我必须使用javascript将其显示为123-456-7890。

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?

谢谢

推荐答案

最快的方法是使用一些正则表达式:

Quickest way would be with some regex:

其中 n 是数字

n.replace(/(\d {3})(\d {3})(\d {4})/,$ 1- $ 2- $ 3);

示例: http://jsfiddle.net/jasongennaro / yXD7g /

var n = "1234567899";
console.log(n.replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3"));

这篇关于在javascript中插入连字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆