Skip to content

Instantly share code, notes, and snippets.

@zmofei
Last active April 22, 2016 09:09
Show Gist options
  • Save zmofei/e5beff1877c92a5c6b9003b7fbf680cc to your computer and use it in GitHub Desktop.
Save zmofei/e5beff1877c92a5c6b9003b7fbf680cc to your computer and use it in GitHub Desktop.
最前端:2016年04月22日题目
题目比较简单,如何把一串数字转换成带分位的货币格式
e.g.
money(123124124) -> 123,124,124
money(123) -> 123
=====
可能的方案之一:
const money = n => ('' + n).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
里面2个点比较值得学习的
1. (?=exp)
这个叫做 “零宽度正预测先行断言”,匹配exp前面的位置
.+?(?=xyz) 可以匹配 uvwxyz的 uvw
2. (?!exp)
这个是“零宽度负预测先行断言”
.+?(?!xyz) 相对于 uvw 和 uvwxyz 的话 只会匹配第一个
@bh-lay
Copy link

bh-lay commented Apr 22, 2016

谢大神贡献,学习了!

@robinma
Copy link

robinma commented Apr 22, 2016

谢谢大神,学习了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment