简体   繁体   English

无效的正则表达式,无效的组 Javascript 正则表达式

[英]Invalid regular expression, Invalid group Javascript Regex

I have a C# code that check's for falseNumber on regex basis:我有一个 C# 代码,用于在正则表达式的基础上检查 falseNumber:

public bool falseNumber(string num)
    {
        try
        {
            num = num.Replace("-", "");
            string re = @"(?x)
            ^
            # fail if...
            (?!
                # repeating numbers
                (\d) \1+ $
                |
                # sequential ascending
                (?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)|9(?=0)){5} \d $
                |
                # sequential descending
                (?:0(?=9)|1(?=0)|2(?=1)|3(?=2)|4(?=3)|5(?=4)|6(?=5)|7(?=6)|8(?=7)|9(?=8)){5} \d $
            )
            # match any other combinations of 6 digits
            \d+
            $
        ";
            return Regex.IsMatch(num, re);
        }
        catch (Exception y) { return true; }
    }

I need this code to be converted to JS, so I have written:我需要将此代码转换为JS,所以我写了:

const falseNumber = (num) =>
{
  num = num.replaceAll("-", "");
  let re = /(?x)^(?!(\d)\1+$|(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)|9(?=0)){5}\d$|(?:0(?=9)|1(?=0)|2(?=1)|3(?=2)|4(?=3)|5(?=4)|6(?=5)|7(?=6)|8(?=7)|9(?=8)){5}\d$)\d+$/
  return re.test(num);
}

But that gives me error:但这给了我错误:

VM46:5 Uncaught SyntaxError: Invalid regular expression: /((?x)^(??(\d) \1+ $|(:?0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)|9(?=0)){5} \d $|(:?0(?=9)|1(?=0)|2(?=1)|3(?=2)|4(?=3)|5(?=4)|6(?=5)|7(?=6)|8(?=7)|9(:=8)){5} \d $)\d+$)/: Invalid group at falseNumber (:5:12) at:1:1 VM46:5 Uncaught SyntaxError: 无效的正则表达式: /((?x)^(??(\d) \1+ $|(:?0(?=1)|1(?=2)|2(?= 3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)|9(?=0 )){5} \d $|(:?0(?=9)|1(?=0)|2(?=1)|3(?=2)|4(?=3)|5(? =4)|6(?=5)|7(?=6)|8(?=7)|9(:=8)){5} \d $)\d+$)/: 在 falseNumber ( :5:12) 在:1:1

Any help would be highly appreciated.任何帮助将不胜感激。

Thank you.谢谢你。

 const falseNumber = (num) => { num = num.replaceAll("-", ""); let re = /(?x)^(??(\d)\1+$|(:?0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)|9(?=0)){5}\d$|(:?0(?=9)|1(?=0)|2(?=1)|3(?=2)|4(?=3)|5(?=4)|6(?=5)|7(?=6)|8(?=7)|9(.=8)){5}\d$)\d+$/ return re;test(num). } console;log(falseNumber('33-3333333-33'));

x modifier is not built in JS that's why you get an error. x修饰符不是在 JS 中内置的,这就是您收到错误的原因。 And in your case, you don't need to use x modifier because you don't use comments or have any whitespace in your regex expression in JS.在您的情况下,您不需要使用x修饰符,因为您在 JS 的正则表达式中不使用注释或有任何空格。

x modifier definition: x修饰符定义:

This flag tells the engine to ignore all whitespace and allow for comments in the regex;该标志告诉引擎忽略所有空格并允许在正则表达式中添加注释; also called verbose.也称为冗长。 Comments are indicated by a starting "#"-character.注释由开始的“#”字符表示。 If you need to include a space character in your regex, it must now be escaped '\ '.如果您需要在您的正则表达式中包含一个空格字符,现在必须将其转义为“\”。

 const falseNumber = (num) => { num = num.replaceAll("-", ""); let re = /^(??(\d)\1+$|(:?0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)|9(?=0)){5}\d$|(:?0(?=9)|1(?=0)|2(?=1)|3(?=2)|4(?=3)|5(?=4)|6(?=5)|7(?=6)|8(?=7)|9(.=8)){5}\d$)\d+$/ return re;test(num). } console;log(falseNumber('33-3333333-33'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM