2

求助一个数字输入的正则表达式

 1 year ago
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.

V2EX  ›  程序员

求助一个数字输入的正则表达式

  zzlit · 4 小时 55 分钟前 · 994 次点击
  1. 00 这种情况不出现输入的时候只展示 0
  2. 01 这种情况展示 1
  3. 0.1 可以出现 现在写了一个正则如下,可以满足前两点,匹配上就后面的替换掉前面的,但是一加上小数点情况就多起来了,怎么写都不太对,求大佬帮帮忙。
const reg = /(?<=^0)\d+/g;
29 条回复    2023-06-05 15:22:12 +08:00
littlepanic72

littlepanic72      4 小时 50 分钟前

[-+]?\b[0-9]*\.?[0-9]+\b regexbuddy 从这个软件找出来的... 安利一个
zhzy

zhzy      4 小时 48 分钟前

其实写不明白的话, 可以考虑不用正则来做吧...
dreasky

dreasky      4 小时 44 分钟前

parseFloat 不就得了
masellum

masellum      4 小时 41 分钟前

这需求不用正则也可以,写成显式的逻辑还更好维护,何必麻烦自己一定要上正则。
xiayushengfan

xiayushengfan      4 小时 33 分钟前

chatgpt
xiayushengfan

xiayushengfan      4 小时 28 分钟前

^(0|[1-9]\d*)(\.\d+)?$
GzhiYi

GzhiYi      4 小时 26 分钟前

有点像是强制输入数值型,比如说价格这种需求。同意楼上说的,获取输入字符后,走 if else 判断一些特殊的输入,最后再走合适的正则。
dinghmcn

dinghmcn      4 小时 23 分钟前

参考 #4 楼的做法,大学的时候做计算器,就解决过类似的问题;使用保存都使用浮点型显示的时候转换成字符串
Ashore

Ashore      4 小时 23 分钟前

/^0*(?:[1-9][0-9]*|0(?:\.[0-9]+)?)$/
loading

loading      4 小时 21 分钟前

如果是 el-input 的话,formatter parser 再结合 holder 应该就可以了,如果是前端,很好做。

你是普通 gui 还是前端
littlepanic72

littlepanic72      4 小时 1 分钟前

@Alias4ck 这个也有点点问题....+0010086 用这样子写的时候他还是会把 00 都认为匹配.... 不加正负号的情况 这个代码就很好了....
littlepanic72

littlepanic72      4 小时 0 分钟前

@Alias4ck +0010086 也通不过这个测试
Alias4ck

Alias4ck      3 小时 57 分钟前

@littlepanic72 再改下就好了 (?![+-]?0\d)[+-]?\d*\.?\d+
Alias4ck

Alias4ck      3 小时 54 分钟前   ❤️ 1

@littlepanic72 其实不会出现你这种情况+001002 / -0023123,你发的这种数字就不太合理
laoyutang

laoyutang      3 小时 48 分钟前 via Android

str.replace(/^0*(?!\.)/,'')
NoOneNoBody

NoOneNoBody      3 小时 43 分钟前

你这是两个需求:校验和替换,各自正则不同,同时实施的话需要有 callback 功能的语言

只考虑替换的话:
^([-+])?(0+)(([1-9][0-9\.]?)|0)$ --> $1$3
如果全 0 带符号的情况,去掉符号的我还要想一下,这个还不行
Pipecraft

Pipecraft      2 小时 51 分钟前   ❤️ 2

把开头连续的 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

bluetree2039      2 小时 48 分钟前

chatpgt 对 正则很 精通~
laoyutang

laoyutang      2 小时 36 分钟前

'00'.replace(/^0(?!\.)/,'')
'0'
'01'.replace(/^0(?!\.)/,'')
'1'
'0.1'.replace(/^0(?!\.)/,'')
'0.1'
zzlit

zzlit      2 小时 21 分钟前

@zhzy
@masellum
@GzhiYi
@dinghmcn 感谢大佬提供另一种思路,只是这里原逻辑是用正则写的,我是进行的优化所以就顺着正则思路想了,我想想看怎么处理好
zzlit

zzlit      2 小时 19 分钟前

@loading 前端,是用 vue 写的指令绑在 input 上的,对输入作限制
zzlit

zzlit      2 小时 18 分钟前

@NoOneNoBody 我本来想的也是这两个部分,拆成两个正则来走规律,但是仔细一想想好像其实用一个正则也可以满足,就是没想好...
xiaoyai0322

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

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

magicyao      54 分钟前

^([1-9][0-9]|0)*[\.]?([0-9]*[1-9])?$

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK