当前位置:网站首页>js正则表达式实现千分位符
js正则表达式实现千分位符
2022-06-22 05:12:00 【兰亭古墨】
function numberThousands (number, thousandsSeperator = ',') {
return String(number).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1' + thousandsSeperator)
}
解释
首先理解一下: 正向肯定预查(look ahead positive assert) 和 正向否定预查(negative assert)
正向肯定预查(look ahead positive assert):
在任何匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如,“Windows(?=95|98|NT|2000)“能匹配"Windows2000"中的"Windows”,但不能匹配"Windows3.1"中的"Windows”。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始。
正向否定预查(negative assert):
在任何不匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如"Windows(?!95|98|NT|2000)“能匹配"Windows3.1"中的"Windows”,但不能匹配"Windows2000"中的"Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始。
/(\d)(?=(\d\d\d)+(?!\d))/g 正则表达式图解:

假设有字符串 12345,正则表达式 (\d)(?=(\d\d\d)+) 首先,匹配 1,发现 1 后面有 234,正向预查到了,所以 1 会被匹配到;再次向后,匹配 2 ,发现 345, 所以 2 也被匹配到,如下图

加上 (?!\d),还要保证正向预查的最后一个字符串 不能为数字
边栏推荐
- Flynk deployment mode summary
- 1108. Defanging an IP Address
- The difference between iqueryable and IEnumerable
- In 2022, the third batch (principal) of Guangdong Provincial Safety Officer a certificate was found and analyzed, and the third batch (principal) of Guangdong Provincial Safety Officer a certificate w
- php正则怎么去掉括号内容
- DTS迁移秘籍-MySQL篇
- 守护进程的流程
- This is a picture
- Geographic location type of redis
- It is easy to analyze and improve R & D efficiency by understanding these five figures
猜你喜欢

6. Local - custom filter factory
![[scientific research notes] focal loss](/img/ca/4a30fd925b87ed2baa2523d8dbf59d.png)
[scientific research notes] focal loss

10道不得不会的 Redis 面试题

Will swift compile to native code- Does Swift compile to native code?

ActiveMQ knowledge summary of Message Oriented Middleware

NLP 的 不可能三角?

Hanoi Tower problem

Progress information collection for progress control of Zhiyuan project management SPM system

In 2022, the super intern plans to make a breakthrough in the offer of it famous enterprises, and the nine high salary skills help the dream of famous enterprises

软件架构与模式:结构、组件、关系
随机推荐
mysql day04课堂笔记
89---狄拉克 delta 函数
C language data type conversion rules (implicit conversion + explicit conversion)
第6章无穷级数_傅立叶级数
QT save qtextedit memory to Txt file
Daemon flow
Storage mode and lifetime of C language variables
Getting the return value of DPI function is always 96 | getting the return value of DPI function is incorrect | why the return value of getdpiformonitor/getdevicecaps is incorrect
In 2022, the third batch (principal) of Guangdong Provincial Safety Officer a certificate was found and analyzed, and the third batch (principal) of Guangdong Provincial Safety Officer a certificate w
DTS迁移秘籍-MySQL篇
MySQL day01 class notes
C语言概念知识扫盲(不定期补充更新)
Free questions for polymerization process and test papers for polymerization process in 2022
Reading notes on how MySQL works [updating]
使用matplotlib实现GUI交互效果
Monorepo Sliding methodology: Reference module Hot Update
Gateway uses global filter for token verification
This is a picture
Six sides tmall has already offered. After a review of my experience, is it really that difficult to enter a big factory?
DTS migration script Oracle