首页 > 语言 > JavaScript > 正文

使用js实现数据格式化

2024-05-06 14:49:45
字体:
来源:转载
供稿:网友

格式化是通过格式操作使任意类型的数据转换成一个字符串。例如下面这样

代码如下:
<script>
console.log(chopper.format('{0} - {1} - {2}', 12, 24, 25)); // outputs "12 - 24 - 25"
</script>

下面是一个完整的代码,可以复制到自己的项目中。

代码如下:
 <!DOCTYPE html>
 <html>
     <head>
         <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
     </head>
     <body>
         <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
         <script>
         (function() {
             var chopper = window.chopper = window.chopper || { cultures: {} },
                 math = Math,
                 formatRegExp = //{(/d+)(:[^/}]+)?/}/g,
                 FUNCTION = "function",
                 STRING = "string",
                 NUMBER = "number",
                 OBJECT = "object",
                 NULL = "null",
                 BOOLEAN = "boolean",
                 UNDEFINED = "undefined",
                 slice = [].slice,
                 globalize = window.Globalize,
                 standardFormatRegExp =  /^(n|c|p|e)(/d*)$/i,
                 literalRegExp = /(//.)|(['][^']*[']?)|(["][^"]*["]?)/g,
                 commaRegExp = //,/g,
                 EMPTY = "",
                 POINT = ".",

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选