我只能允许“ - ” - "在Regex中的第一个字符?

发布于 2025-02-12 15:40:18 字数 378 浏览 1 评论 0原文

我正在使用反应 我做了一个只接受数字的正则罚款。在这里,我只想一开始就允许一个减去。也就是说,我想创建一个允许“ - ”仅一次但可以使用或不使用的正则表达式。因此,我应用了我的代码,但它不起作用。如何修复正则表达式?

限制是,不得仅在数字开始时,不得放在数字的中间或之后,并且可以使用负或不使用。

       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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

芸娘子的小脾气 2025-02-19 15:40:18

希望以下内容能够解决您的问题(如果您想制作负标志的材料):

/(-)([0-9]{1,4})/

如果您想使减号可选,请​​尝试以下操作:

/([-0-9]{1,4})/

如果其中任何一个都不适合您,请询问,我会问相应地为你做一个。

Hopefully the following will fix your issue(in case you want to make minus sign compulosry):

/(-)([0-9]{1,4})/

In case you want to make minus sign optional, please try this:

/([-0-9]{1,4})/

If either of these don't work for you, please ask and I'll make one accordingly for you.

作业与我同在 2025-02-19 15:40:18

您可以在以下以下:

EXP 1:
/^(-)? \ d +$/g.test("-213213“)

exp 2:
/^(0| (-)? [1-9] \ d*):frice/g.test("12321“)

  • ” - “与?使其非强制性
  • D+期望超过1位数字
  • EXP 1允许以0的启动:“ -0”或“ 0123”也将是正确的。如果您想限制这一点。使用EXP 2

You can use below regEx:

Exp 1:
/^(-)?\d+$/g.test("-213213")

Exp 2:
/^(0|(-)?[1-9]\d*)$/g.test("12321")

  • "-" with ? makes it non-mandatory
  • d+ expects more than 1 digits
  • Exp 1 allows start with 0 for ex: "-0" or "0123" will also be true. If you want to restrict that. Use Exp 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文