javascript使用正则表达式格式化货币,金额
来源:互联网 发布:苹果cms模板论坛 编辑:程序博客网 时间:2024/06/07 07:03
var val='212312.235423'
var rex = /\d{1,3}(?=(\d{3})+$)/g;
val.replace(/^(-?)(\d+)((\.\d+)?)$/, function (s, s1, s2, s3) {
return '$' + s1 + s2.replace(rex, '$&,') + s3;
})
//"$212,312.235423"
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
str.replace(regexp|substr, newSubStr|function[, flags])
Parameters
--regexp (pattern)
A RegExp object. The match is replaced by the return value of parameter #2.
--substr (pattern)
A String that is to be replaced by newSubStr.
--newSubStr (replacement)
The String that replaces the substring received from parameter #1. A number of special replacement patterns are supported; see the "Specifying a string as a parameter" section below.
--function (replacement)
A function to be invoked to create the new substring (to put in place of the substring received from parameter #1). The arguments supplied to this function are described in the "Specifying a function as a parameter" section below.
--flags
Note: The flags argument does not work in v8 Core (Chrome and Node.js). A string specifying a combination of regular expression flags. The use of the flags parameter in the String.prototype.replace() method is non-standard and deprecated. Instead of using this parameter, use a RegExp object with the corresponding flags. The value of this parameter if it is used should be a string consisting of one or more of the following characters to affect the operation as described:
g:global match
i:ignore case
m:match over multiple lines
y:sticky
Parameters
--regexp (pattern)
A RegExp object. The match is replaced by the return value of parameter #2.
--substr (pattern)
A String that is to be replaced by newSubStr.
--newSubStr (replacement)
The String that replaces the substring received from parameter #1. A number of special replacement patterns are supported; see the "Specifying a string as a parameter" section below.
--function (replacement)
A function to be invoked to create the new substring (to put in place of the substring received from parameter #1). The arguments supplied to this function are described in the "Specifying a function as a parameter" section below.
--flags
Note: The flags argument does not work in v8 Core (Chrome and Node.js). A string specifying a combination of regular expression flags. The use of the flags parameter in the String.prototype.replace() method is non-standard and deprecated. Instead of using this parameter, use a RegExp object with the corresponding flags. The value of this parameter if it is used should be a string consisting of one or more of the following characters to affect the operation as described:
g:global match
i:ignore case
m:match over multiple lines
y:sticky
PatternInserts
$$ Inserts a "$".
$& Inserts the matched substring.
$` Inserts the portion of the string that precedes the matched substring.
$' Inserts the portion of the string that follows the matched substring.
$n or $nn Where n or nn are decimal digits, inserts the nth parenthesized submatch string, provided the first argument was a RegExp object.
$$ Inserts a "$".
$& Inserts the matched substring.
$` Inserts the portion of the string that precedes the matched substring.
$' Inserts the portion of the string that follows the matched substring.
$n or $nn Where n or nn are decimal digits, inserts the nth parenthesized submatch string, provided the first argument was a RegExp object.
0 0
- javascript使用正则表达式格式化货币,金额
- javascript/js 正则表达式格式化金额
- JS 货币金额格式化
- javascript 用正则表达式对金额验证
- 使用Javascript正则表达式来格式化XML内容
- Javascript 货币格式化输出
- JAVASCRIPT 货币格式化
- javascript 中的货币格式化
- `JavaScript`格式化货币数字
- Javascript 格式化数字,金额
- 用正则表达式检验日期,身份证,金额,邮箱,格式化日期,转换金额格式,js 大全
- 用正则表达式检验日期,身份证,金额,邮箱,格式化日期,转换金额格式,js 大全
- 货币正则表达式
- 正则表达式-匹配货币
- 金额正则表达式
- 金额的正则表达式
- 金额的正则表达式
- 金额正则表达式
- 深入掌握JMS(七):DeliveryMode例子
- scala学习第二章 控制结构与函数
- 网络数据分析器
- java synchronized详解
- 最快的N匹马
- javascript使用正则表达式格式化货币,金额
- C#单例模式的三种写法
- arm-linux-gdb与gdbserver
- PL/SQL语句学习之使用while、loop和for三种循环打印数字的1-10
- IPV4与IPV6 的 一点区别 (不深究、只记录备忘)
- 深入掌握JMS(八):JMSReplyTo
- [leetcode] Ugly Number
- 数据结构 C语言实现快速排序
- java版的memcached client及使用文档