当前位置:网站首页>JS regular expression to implement the thousands separator
JS regular expression to implement the thousands separator
2022-06-22 05:15:00 【Lanting ancient ink】
function numberThousands (number, thousandsSeperator = ',') {
return String(number).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1' + thousandsSeperator)
}
explain
First of all, understand : Positive positive preview (look ahead positive assert) and Positive negative pre check (negative assert)
Positive positive preview (look ahead positive assert):
In any match pattern Match at the beginning of the string to find the string . This is a non fetch match , in other words , The match does not need to be retrieved for later use . for example ,“Windows(?=95|98|NT|2000)“ Can match "Windows2000" Medium "Windows”, But can't match "Windows3.1" Medium "Windows”. Pre check does not consume characters , in other words , After a match happens , Start the search for the next match immediately after the last match , Instead of starting after the character that contains the prefetch .
Positive negative pre check (negative assert):
In any mismatch pattern Match at the beginning of the string to find the string . This is a non fetch match , in other words , The match does not need to be retrieved for later use . for example "Windows(?!95|98|NT|2000)“ Can match "Windows3.1" Medium "Windows”, But can't match "Windows2000" Medium "Windows". Pre check does not consume characters , in other words , After a match happens , Start the search for the next match immediately after the last match , Instead of starting after the character that contains the prefetch .
/(\d)(?=(\d\d\d)+(?!\d))/g Regular expression diagram :

Assuming a string 12345, Regular expressions (\d)(?=(\d\d\d)+) First , matching 1, Find out 1 In the back 234, The forward preview has found , therefore 1 Will be matched to ; Back again , matching 2 , Find out 345, therefore 2 Also matched to , Here's the picture

add (?!\d), Also make sure that the last string of the forward look-up It can't be numbers
边栏推荐
- DTS迁移秘籍-SQLSERVER篇
- [user guide] use of Tsinghua source
- Software architecture and pattern: structure, component and relationship
- 软件架构与模式:结构、组件、关系
- Common knowledge arrangement of numpy database
- 畢業回饋!Apache Doris 社區所有貢獻者來領禮品啦!
- 守护进程的流程
- flink部署模式总结
- Tupu software 2D and 2.5D case collection | smart Park, data center, SMT production line
- Monorepo silk slide methodology: reference module hot update
猜你喜欢

并发编程——线程池

9 practical shell scripts, recommended collection!

When idea creates a method, it uses annotations to prompt method parameters (param), return value (return), and method function (description)

Contents of 2022 tea master (intermediate) examination and tea master (intermediate) examination

Kubernetes——裸机搭建集群环境

Rétroaction sur la remise des diplômes! Tous les contributeurs de la communauté Apache Doris sont ici pour recevoir des cadeaux!
![[camp] at the beginning, improve [leopard] power - vivo activity plug-in management platform](/img/75/6a129de59c21b783622ba31f77f647.jpg)
[camp] at the beginning, improve [leopard] power - vivo activity plug-in management platform

This is a picture

Talk about MySQL's locking rule "hard hitting MySQL series 15"

Geographic location type of redis
随机推荐
非递归打印斐波那契数列
Monorepo絲滑方法論:引用模塊熱更新
Use keytool to access the JKS file get SSL certificate
How much is London gold
C语言变量的存储方式和生存期
jedispool工具类
从JedisSentinelPool获取jedis
Remote Dictionary Server(Redis)——基于 KV 结构的作为 Cache 使用的 NoSQL 数据库管理系统
DTS迁移秘籍-SQLSERVER篇
数据库---基础知识
Lua notes
Talk about MySQL's locking rule "hard hitting MySQL series 15"
冰河十年前的预测如今被阿里实现了,非常震撼
守护进程的流程
1108. Defanging an IP Address
Qt保存QTextEdit内存至.txt文件中
flink部署模式(二)- yarn三种部署模式
TIDB-升级版本
9 practical shell scripts, recommended collection!
mysql day02课堂笔记