【问题标题】:match pattern in RegEx in jsjs中RegEx中的匹配模式
【发布时间】:2020-11-07 19:37:09
【问题描述】:

我想编辑一个 pattern 来检查文本是否以 number/s 开头并以 number/soperator 结尾 并根据 test() 方法返回 true 或 false。

如何使用 JavaScript 做到这一点?`

var str= "123+125",str2 = "123",str3 = "123*";
let RegularExpression = /^\d*(\d*|(\+|\-|\*|\/){1,})$/; //this pattern variable what I want to edit because it doesn't do what I want. 
let result1 = RegularExpression.test(str);
let result2 = RegularExpression.test(str);
let result2 = RegularExpression.test(str);

【问题讨论】:

  • 欢迎来到 StackOverflow。目前还不清楚您希望您的正则表达式匹配什么,以及您如何测试它,例如您没有使用str2str3 变量,而是绑定了result2 两次。或许可以考虑查看一些关于 JS 中正则表达式的文档,参见例如developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

标签: javascript php html regex dom


【解决方案1】:

使用

/^\d+([-+*\/]\d+)*[-+*\/]?$/

proof

解释:

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  \d+                      digits (0-9) (1 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1 (0 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    [-+*\/]                  any character of: '-', '+', '*', '\/'
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )*                       end of \1 (NOTE: because you are using a
                           quantifier on this capture, only the LAST
                           repetition of the captured pattern will be
                           stored in \1)
--------------------------------------------------------------------------------
  [-+*\/]?                 any character of: '-', '+', '*', '\/'
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

JavaScript:

var str= "123+125",str2 = "123",str3 = "123*";
let RegularExpression = /^\d+([-+*\/]\d+)*[-+*\/]?$/;
console.log( RegularExpression.test(str) );
console.log( RegularExpression.test(str2) );
console.log( RegularExpression.test(str3) );

【讨论】:

    【解决方案2】:

    您可以使用重复 1 次或多次匹配 1 个或多个数字,可选地后跟一个运算符。

    ^(?:\d+[+*/-]?)+
    

    说明

    • ^ 字符串开始
    • (?:非捕获组
      • \d+[+*/-]? 匹配 1+ 位可选地后跟 + * / -
    • )+关闭非捕获组并重复1次以上

    Regex demo

    let pattern = /^(?:\d+[+*/-]?)+$/m;
    [
      "123+125",
      "",
      "123",
      "123*",
      "123+125-1"
    ].forEach(s => console.log(s + " --> " + pattern.test(s)));

    【讨论】:

      【解决方案3】:

      试试这个模式/^(\d*|[+/*-]?){1,}$/

      var str= "123+125",str2 = "123",str3 = "123*";
      let RegularExpression = /^(\d*|[+/*-]?){1,}$/; //this pattern variable what I want to edit because it doesn't do what I want. 
      let result1 = RegularExpression.test(str);
      let result2 = RegularExpression.test(str);
      let result3 = RegularExpression.test(str);
      
      console.log("res1:",result1,",res3:",result2,",res3:",result3)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多