Last active
April 22, 2016 09:09
-
-
Save zmofei/e5beff1877c92a5c6b9003b7fbf680cc to your computer and use it in GitHub Desktop.
最前端:2016年04月22日题目
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
题目比较简单,如何把一串数字转换成带分位的货币格式 | |
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 的话 只会匹配第一个 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
谢大神贡献,学习了!