需要满足
const reg = /(?<=^0)\d+/g;
1
littlepanic72 110 天前
[-+]?\b[0-9]*\.?[0-9]+\b regexbuddy 从这个软件找出来的... 安利一个
|
2
littlepanic72 110 天前
@littlepanic72 好像不行...
|
![]() |
3
zhzy 110 天前
其实写不明白的话, 可以考虑不用正则来做吧...
|
![]() |
4
dreasky 110 天前
parseFloat 不就得了
|
5
masellum 110 天前
这需求不用正则也可以,写成显式的逻辑还更好维护,何必麻烦自己一定要上正则。
|
6
Alias4ck 110 天前
|
7
xiayushengfan 110 天前
chatgpt
|
8
Alias4ck 110 天前
|
9
xiayushengfan 110 天前
^(0|[1-9]\d*)(\.\d+)?$
|
![]() |
10
GzhiYi 110 天前
有点像是强制输入数值型,比如说价格这种需求。同意楼上说的,获取输入字符后,走 if else 判断一些特殊的输入,最后再走合适的正则。
|
![]() |
11
dinghmcn 110 天前
参考 #4 楼的做法,大学的时候做计算器,就解决过类似的问题;使用保存都使用浮点型显示的时候转换成字符串
|
![]() |
12
Ashore 110 天前
/^0*(?:[1-9][0-9]*|0(?:\.[0-9]+)?)$/
|
![]() |
13
loading 110 天前
如果是 el-input 的话,formatter parser 再结合 holder 应该就可以了,如果是前端,很好做。
你是普通 gui 还是前端 |
14
littlepanic72 110 天前
@Alias4ck 这个也有点点问题....+0010086 用这样子写的时候他还是会把 00 都认为匹配.... 不加正负号的情况 这个代码就很好了....
|
15
littlepanic72 110 天前
@Alias4ck +0010086 也通不过这个测试
|
16
Alias4ck 110 天前
@littlepanic72 再改下就好了 (?![+-]?0\d)[+-]?\d*\.?\d+
|
17
Alias4ck 110 天前 ![]() @littlepanic72 其实不会出现你这种情况+001002 / -0023123,你发的这种数字就不太合理
|
![]() |
18
laoyutang 110 天前 via Android
str.replace(/^0*(?!\.)/,'')
|
19
NoOneNoBody 110 天前
你这是两个需求:校验和替换,各自正则不同,同时实施的话需要有 callback 功能的语言
只考虑替换的话: ^([-+])?(0+)(([1-9][0-9\.]?)|0)$ --> $1$3 如果全 0 带符号的情况,去掉符号的我还要想一下,这个还不行 |
![]() |
20
Pipecraft 110 天前 ![]() 把开头连续的 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 ``` |
21
bluetree2039 110 天前
chatpgt 对 正则很 精通~
|
![]() |
22
laoyutang 110 天前
'00'.replace(/^0(?!\.)/,'')
'0' '01'.replace(/^0(?!\.)/,'') '1' '0.1'.replace(/^0(?!\.)/,'') '0.1' |
![]() |
23
zzlit OP |
![]() |
24
zzlit OP |
![]() |
26
zzlit OP @NoOneNoBody 我本来想的也是这两个部分,拆成两个正则来走规律,但是仔细一想想好像其实用一个正则也可以满足,就是没想好...
|
27
xiaoyai0322 110 天前
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') |
28
xiaoyai0322 110 天前
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') |
![]() |
29
magicyao 110 天前
^([1-9][0-9]|0)*[\.]?([0-9]*[1-9])?$
|
![]() |
30
wuwukai007 109 天前
gpt4: /^0*(0\.|[1-9])/
|
![]() |
31
nzbin 109 天前
很久以前做过一个类似的需求。。。
https://www.cnblogs.com/nzbin/p/6742528.html |