![](/style/images/good.png)
![](/style/images/bad.png)
求助一个数字输入的正则表达式
source link: https://www.v2ex.com/t/945862
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
- 00 这种情况不出现输入的时候只展示 0
- 01 这种情况展示 1
- 0.1 可以出现 现在写了一个正则如下,可以满足前两点,匹配上就后面的替换掉前面的,但是一加上小数点情况就多起来了,怎么写都不太对,求大佬帮帮忙。
const reg = /(?<=^0)\d+/g;
littlepanic72 4 小时 50 分钟前 [-+]?\b[0-9]*\.?[0-9]+\b regexbuddy 从这个软件找出来的... 安利一个
|
![]() |
zhzy 4 小时 48 分钟前 其实写不明白的话, 可以考虑不用正则来做吧...
|
![]() |
dreasky 4 小时 44 分钟前 parseFloat 不就得了
|
masellum 4 小时 41 分钟前 这需求不用正则也可以,写成显式的逻辑还更好维护,何必麻烦自己一定要上正则。
|
xiayushengfan 4 小时 33 分钟前 chatgpt
|
xiayushengfan 4 小时 28 分钟前 ^(0|[1-9]\d*)(\.\d+)?$
|
![]() |
GzhiYi 4 小时 26 分钟前 有点像是强制输入数值型,比如说价格这种需求。同意楼上说的,获取输入字符后,走 if else 判断一些特殊的输入,最后再走合适的正则。
|
![]() |
dinghmcn 4 小时 23 分钟前 参考 #4 楼的做法,大学的时候做计算器,就解决过类似的问题;使用保存都使用浮点型显示的时候转换成字符串
|
![]() |
Ashore 4 小时 23 分钟前 /^0*(?:[1-9][0-9]*|0(?:\.[0-9]+)?)$/
|
![]() |
loading 4 小时 21 分钟前 如果是 el-input 的话,formatter parser 再结合 holder 应该就可以了,如果是前端,很好做。
你是普通 gui 还是前端 |
littlepanic72 4 小时 1 分钟前 @Alias4ck 这个也有点点问题....+0010086 用这样子写的时候他还是会把 00 都认为匹配.... 不加正负号的情况 这个代码就很好了....
|
littlepanic72 4 小时 0 分钟前 @Alias4ck +0010086 也通不过这个测试
|
Alias4ck 3 小时 57 分钟前 @littlepanic72 再改下就好了 (?![+-]?0\d)[+-]?\d*\.?\d+
|
Alias4ck 3 小时 54 分钟前 @littlepanic72 其实不会出现你这种情况+001002 / -0023123,你发的这种数字就不太合理
|
![]() |
laoyutang 3 小时 48 分钟前 via Android str.replace(/^0*(?!\.)/,'')
|
NoOneNoBody 3 小时 43 分钟前 你这是两个需求:校验和替换,各自正则不同,同时实施的话需要有 callback 功能的语言
只考虑替换的话: ^([-+])?(0+)(([1-9][0-9\.]?)|0)$ --> $1$3 如果全 0 带符号的情况,去掉符号的我还要想一下,这个还不行 |
![]() |
Pipecraft 2 小时 51 分钟前 把开头连续的 0 去掉就可以。
``` function removeLeadingZero(num) { const regex = /^([+-]?)0+(?=\d)/ return num.replace(regex, "$1") } console.log(removeLeadingZero("00")) // 0 console.log(removeLeadingZero("000")) // 0 console.log(removeLeadingZero("01")) // 1 console.log(removeLeadingZero("001")) // 1 console.log(removeLeadingZero("0.1")) // 0.1 console.log(removeLeadingZero("+0.1")) // +0.1 console.log(removeLeadingZero("-0.1")) // -0.1 console.log(removeLeadingZero("00.1")) // 0.1 console.log(removeLeadingZero("0012340012")) // 12340012 console.log(removeLeadingZero("+0010086")) // +10086 console.log(removeLeadingZero("-0010086")) // -10086 console.log(removeLeadingZero("+000000")) // +0 ``` |
bluetree2039 2 小时 48 分钟前 chatpgt 对 正则很 精通~
|
![]() |
laoyutang 2 小时 36 分钟前 '00'.replace(/^0(?!\.)/,'')
'0' '01'.replace(/^0(?!\.)/,'') '1' '0.1'.replace(/^0(?!\.)/,'') '0.1' |
![]() |
zzlit 2 小时 21 分钟前 |
![]() |
zzlit 2 小时 18 分钟前 @NoOneNoBody 我本来想的也是这两个部分,拆成两个正则来走规律,但是仔细一想想好像其实用一个正则也可以满足,就是没想好...
|
xiaoyai0322 1 小时 14 分钟前 replace(/^0*/g, '').replace(/[^\d.]/g, '')
.replace(/\.{2,}/g, '.').replace('.', '$#$') .replace(/\./g, '').replace('$#$', '.') .replace(/^\./g, '0.') .replace(new RegExp("^(\\-)*(\\d+)\\.(" + '\\d'.repeat(n) + ").*$"), '$1$2.$3') |
xiaoyai0322 1 小时 4 分钟前 replace(/^0*/g, '').replace(/[^\d.]/g, '')
.replace(/\.{2,}/g, '.').replace('.', '$#$') .replace(/\./g, '').replace('$#$', '.') .replace(/^\./g, '0.') 最后那个是保留几位小数 //.replace(new RegExp("^(\\-)*(\\d+)\\.(" + '\\d'.repeat(n) + ").*$"), '$1$2.$3') |
![]() |
magicyao 54 分钟前 ^([1-9][0-9]|0)*[\.]?([0-9]*[1-9])?$
|
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK