我只能允许“ - ” - "在Regex中的第一个字符?
我正在使用反应 我做了一个只接受数字的正则罚款。在这里,我只想一开始就允许一个减去。也就是说,我想创建一个允许“ - ”仅一次但可以使用或不使用的正则表达式。因此,我应用了我的代码,但它不起作用。如何修复正则表达式?
限制是,不得仅在数字开始时,不得放在数字的中间或之后,并且可以使用负或不使用。
let minCheck = !/^[0-9]{1,4}$/g.test("-1")
我尝试过
let minCheck = /-?!/^[0-9]{1,4}$/g.test(valueRange.minValue)
,但它不起作用,
我该如何修复我的代码?
I'm using React-native
I made a regex that accepts only numbers. Here, I want to allow one minus at the beginning only. That is, I want to create a regular expression that allows "-" only once, but can be used or not. So I applied my code, but it doesn't work. How do I fix my regular expression?
The restriction is that the minus must not be placed in the middle of or after the number, only at the beginning of the number, and the minus can be used or not.
let minCheck = !/^[0-9]{1,4}$/g.test("-1")
i tried
let minCheck = /-?!/^[0-9]{1,4}$/g.test(valueRange.minValue)
but it doesn't work
How can i fix my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
希望以下内容能够解决您的问题(如果您想制作负标志的材料):
如果您想使减号可选,请尝试以下操作:
如果其中任何一个都不适合您,请询问,我会问相应地为你做一个。
Hopefully the following will fix your issue(in case you want to make minus sign compulosry):
In case you want to make minus sign optional, please try this:
If either of these don't work for you, please ask and I'll make one accordingly for you.
您可以在以下以下:
EXP 1:
/^(-)? \ d +$/g.test("-213213“)
exp 2:
/^(0| (-)? [1-9] \ d*):frice/g.test("12321“)
You can use below regEx:
Exp 1:
/^(-)?\d+$/g.test("-213213")
Exp 2:
/^(0|(-)?[1-9]\d*)$/g.test("12321")