【问题标题】:Regular expression that only accepts numbers and certain characters under certain conditions with javascript (Vue.js) [closed]使用javascript(Vue.js)在某些条件下仅接受数字和某些字符的正则表达式[关闭]
【发布时间】:2020-10-26 14:50:54
【问题描述】:

任何人都可以帮助我使用执行以下操作的正则表达式:

  • 只接受 0-9 的数字
  • 仅接受以下只能出现一次的字符:“e”、“”和“.”
  • 输入句号时,不能再输入逗号,反之亦然
  • “e”后不得输入逗号或句点

我希望有人可以帮助我,我自己不擅长正则表达式,但知道它们很强大。但是,如果有其他方法可用,请告诉我。 我自己通过不同的方法尝试过。我写的不幸不能正常工作的代码如下:

validateNumberInput() {
  this.countPeriods = 0
  this.countCommas = 0
  this.countE = 0
  for (var i = 0; i <= this.inputValue.length; i++) {
    if (
      isNaN(this.inputValue[i]) &&
      this.inputValue[i] &&
      this.inputValue[i] != "." &&
      this.inputValue[i] != "," &&
      this.inputValue[i].toUpperCase() != "E"
    ) {
      console.log(this.inputValue[i])
      console.log(this.inputValue.length)
      this.inputValue = this.inputValue.replace(this.inputValue[i], "")
    } else if (this.inputValue[i] == "." && this.countPeriods < 1) {
      this.countPeriods = this.countPeriods + 1
      this.countCommas = this.countCommas + 1
    } else if (this.inputValue[i] == "." && this.countPeriods >= 1) {
      this.inputValue = this.inputValue.replace(this.inputValue[i], "")
    } else if (this.inputValue[i] == ",") {
      this.countCommas = this.countCommas + 1
      this.countPeriods = this.countPeriods + 1
    } else if (this.inputValue[i] == "E" || this.inputValue[i] == "e") {
      this.countE = this.countE + 1
    }

    //  Only accept a period, comma or 'e' once
    if (this.inputValue[i] == "." && this.countPeriods > 1) {
      this.inputValue = this.inputValue.replace(this.inputValue[i], "")
    } else if (this.inputValue[i] == "," && this.countCommas > 1) {
      this.inputValue = this.inputValue.replace(this.inputValue[i], "")
    } else if ((this.inputValue[i] == "E" || this.inputValue[i] == "e") && this.countE > 1) {
      this.inputValue = this.inputValue.replace(this.inputValue[i], "")
    }
  }
},

我遇到的问题是,如果句号、逗号或“e”已经出现过一次,并且我重新输入其中一个字符,则该字符会在其原始位置消失并被新字符替换。应该是这样的,如果已经输入了这些字符之一,我就不能再输入这些字符了

【问题讨论】:

  • 你的代码有一个bug:当你解析输入“abc”时,i=0,你删除了“a”,留下了inputValue="bc"。现在你前进到 i=1,检查“c”,删除它留下 inputValue="b"。现在您访问 inputValue[1] 来测试周期。程序崩溃。要更正,您可以将 inputValue[i] 存储在临时变量中,并且仅在测试表达式中使用此变量。

标签: javascript numbers character expression


【解决方案1】:

验证字符串是数字还是匹配规则的正则表达式是^[0-9]*[.,]?[0-9]+([eE][0-9]+)?$。我在https://www.regular-expressions.info/floatingpoint.html 找到它。你可以在那里阅读它是如何工作的。我只替换了“.?”使用 [.,]?,因为您想匹配逗号或分号作为分形部分的分隔符。 我还省略了“[-+]?”到处都是,因为您不想匹配代数符号(在指数或整数前面)。

但是……

在您发布的代码中,您尝试删除所有无效字符,这意味着将例如“a12a3.4x5.6e7E8”转换为“123.456e78”。 您也可以为此使用单个正则表达式,但它会非常复杂,以至于没有人会立即理解它。因此,我建议改用以下三个步骤:

var s = "1a2.3b4.5,6e78Ee.";
var result = s.replaceAll(/[^0-9.,e]/gi, ""); // delete all characters except numbers and ,.E
console.log(result); // "12.34.5,6e78Ee."
result = result.replaceAll(/(?<=[.,e].*)[.,]/gi, ""); // delete all dots and commas that are preceded by other dots, commas or exponent.
console.log(result); // "12.3456e78Ee" 
result = result.replaceAll(/(?<=e.*)e/gi, ""); // keep the first exponent and delete all others
console.log(result); // "12.3456e78"

如果您关心减号,可以添加第四步,请参见下面的代码。使用的时候别忘了第一步在正则表达式内的允许字符列表中加上减号。

var s = "-12-34.-56e-7-8";
var result = s.replaceAll(/(?<!e|^)-/gi, ""); // delete minus signs if it is not directly preceded by an E, or if it does not stand at the beginning
console.log(result); // "-1234.56e-78"

【讨论】:

  • 非常感谢!你的代码帮助了我。我将尝试在正则表达式中进一步发展自己,它们真的很有用
  • 嘿,我还有一个小问题。也许您可以帮助我解决以下问题:减号在整个字符串中出现的次数不得超过两次。在“e”之前,一个减号最多可以出现 2 次。在“e”之后,最多允许 1x。我部分成功地在 e 之后只接受了一次减号。不幸的是,我还没有设法接受整个字符串最大 2x 的减号
  • @AliAls 我为减号添加了一个解决方案。如果您对此感到满意,请将我的解决方案标记为已接受的答案。
  • 谢谢你的好代码,它对我帮助很大
猜你喜欢
  • 1970-01-01
  • 2011-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-08
  • 1970-01-01
相关资源
最近更新 更多