Open ahalf-yuan opened 5 years ago
金钱格式化:1234567890 --> 1,234,567,890; 中的正则/\B(?=(\d{3})+(?!\d))/g
?!
:
正向否定断言,表示连续三个数字之后不能存在数字。
所以,123456 不满足条件,但是234567满足,因为7之后不存在数字。
?=
:
(?=exp)也叫零宽度正预测先行断言,它断言自身出现的位置的后面能匹配表达式exp。
可以理解为?=是一个狂妄的预言家,假设有正则 /abc?=xxx/,它预言自己出现的位置后面一定、肯定、必须、只能是 xxx,如果不是,那它会以死明志的(就是匹配失败)。
所以,?=(\d{3})+(?!\d)
匹配三个数字的一组或多组数字 且 不能以数字结尾 的位置。
replace(/(\d{3})(?=[^$])/g, "$1,")
?=
与?:
的区别 \B
- 一个非单词边界
这些JavaScript编程黑科技,装逼指南,高逼格代码,让你惊叹不已
1.Tiny Rate
包含小数的实现 ★构建东半球最小的评级组件☆
2.JavaScript 错误处理的方式的正确姿势
跳转到 stackoverflow 网站。
3.从一行代码里面学点JavaScript
将页面所有的元素用不同颜色的1px 的边框包裹。
翻译成正常语言
4.论如何优雅的取随机字符串
5.论如何优雅的取整
6.如何优雅的实现金钱格式化:1234567890 --> 1,234,567,890
7.用最短的代码实现一个长度为m(6)且值都n(8)的数组
8.取出一个数组中的最大值和最小值