简体   繁体   English

为什么相同的一次性正则表达式在两个平台上表现不同?

[英]Why does the same single-use regex behave differently in two platforms?

I'm attempting to validate an RFC (Mexican tax id) code in a form with JS and JQuery.我正在尝试使用 JS 和 JQuery 的形式验证 RFC(墨西哥税号)代码。 I only require to validate whether the following Regex match returns null :我只需要验证以下正则表达式匹配是否返回null

 function validateRFC(rfc) { const rgxFisica = "^(([A-ZÑ&]{4})([0-9]{2})([0][13578]|[1][02])(([0][1-9]|[12][\\d])|[3][01])([A-Z0-9]{3}))|" + "(([A-ZÑ&]{4})([0-9]{2})([0][13456789]|[1][012])(([0][1-9]|[12][\\d])|[3][0])([A-Z0-9]{3}))|" + "(([A-ZÑ&]{4})([02468][048]|[13579][26])[0][2]([0][1-9]|[12][\\d])([A-Z0-9]{3}))|" + "(([A-ZÑ&]{4})([0-9]{2})[0][2]([0][1-9]|[1][0-9]|[2][0-8])([A-Z0-9]{3}))$"; return rfc.match(rgxFisica);= null } const foo = validateRFC("XXXX901212UDX"). console;log(foo);

The pattern is basically XXXXYYMMDDYYY where X must be a letter, YMD are date values and Y can be any alphanumeric character.该模式基本上是XXXXYYMMDDYYY ,其中X必须是字母, YMD是日期值, Y可以是任何字母数字字符。

I did a small unit test on codepen with my real id and it works.我用我的真实身份对 codepen 做了一个小单元测试,它可以工作。 However, when testing it in my test environment it always returns null.但是,在我的测试环境中对其进行测试时,它总是返回 null。 Here's a screenshot.这是一个屏幕截图。 In case it matters, I'm rendering the entire page with Flask in Python 3.7, and the validation is inside document.ready万一这很重要,我在 Python 3.7 中使用 Flask 渲染整个页面,并且验证在document.ready

Is it possible that my Javascript is interpreting regular expressions differently?我的 Javascript 是否有可能以不同的方式解释正则表达式?

EDIT:编辑:

It seems the embedded snippet also returns true .嵌入的代码段似乎也返回true I'm using VS2019 16.3.5, Python 16.3.19252.1 in a Python 3.7 64-bit environment.我在 Python 3.7 64 位环境中使用 VS2019 16.3.5, Python 16.3.19252.1。

In some cases of javascript, like ECMAScript, if the match fails, then the call rfc.match(rgxFisica) will return an array.在 javascript 的某些情况下,如 ECMAScript,如果匹配失败,则调用rfc.match(rgxFisica)将返回一个数组。 If the match passes, it will return a boolean true .如果匹配通过,它将返回 boolean true

see example ( https://jsfiddle.net/coq3Lugw/ ):参见示例( https://jsfiddle.net/coq3Lugw/ ):

function validateRFC(rfc) {
  const rgxFisica = "^(([A-ZÑ&]{4})([0-9]{2})([0][13578]|[1][02])(([0][1-9]|[12][\\d])|[3][01])([A-Z0-9]{3}))|" +
    "(([A-ZÑ&]{4})([0-9]{2})([0][13456789]|[1][012])(([0][1-9]|[12][\\d])|[3][0])([A-Z0-9]{3}))|" +
    "(([A-ZÑ&]{4})([02468][048]|[13579][26])[0][2]([0][1-9]|[12][\\d])([A-Z0-9]{3}))|" +
    "(([A-ZÑ&]{4})([0-9]{2})[0][2]([0][1-9]|[1][0-9]|[2][0-8])([A-Z0-9]{3}))$";
    console.log(rfc.match(rgxFisica))

    console.log("hello 0670 world".match(rgxFisica))

  return rfc.match(rgxFisica) != null
}

const foo = validateRFC("XXXX901212UDX");
//console.log(foo);

what I would do is set an if-then statement and return that way:我要做的是设置一个 if-then 语句并以这种方式返回:

function validateRFC(rfc) {
  const rgxFisica = "^(([A-ZÑ&]{4})([0-9]{2})([0][13578]|[1][02])(([0][1-9]|[12][\\d])|[3][01])([A-Z0-9]{3}))|" +
    "(([A-ZÑ&]{4})([0-9]{2})([0][13456789]|[1][012])(([0][1-9]|[12][\\d])|[3][0])([A-Z0-9]{3}))|" +
    "(([A-ZÑ&]{4})([02468][048]|[13579][26])[0][2]([0][1-9]|[12][\\d])([A-Z0-9]{3}))|" +
    "(([A-ZÑ&]{4})([0-9]{2})[0][2]([0][1-9]|[1][0-9]|[2][0-8])([A-Z0-9]{3}))$";

  if (rfc.match(rgxFisica)) == null{
      return True
  } else {
      return False
  }
}

const foo = validateRFC("XXXX901212UDX");
//console.log(foo);

An example of a TRUE regex match:一个 TRUE 正则表达式匹配的例子:

function validateRFC(rfc) {
  const rgxFisica = "^(([A-ZÑ&]{4})([0-9]{2})([0][13578]|[1][02])(([0][1-9]|[12][\\d])|[3][01])([A-Z0-9]{3}))|" +
    "(([A-ZÑ&]{4})([0-9]{2})([0][13456789]|[1][012])(([0][1-9]|[12][\\d])|[3][0])([A-Z0-9]{3}))|" +
    "(([A-ZÑ&]{4})([02468][048]|[13579][26])[0][2]([0][1-9]|[12][\\d])([A-Z0-9]{3}))|" +
    "(([A-ZÑ&]{4})([0-9]{2})[0][2]([0][1-9]|[1][0-9]|[2][0-8])([A-Z0-9]{3}))$";

 console.log(rfc.match(rgxFisica));

  if (rfc.match(rgxFisica) == null){
      return true
  } else{
      return false
  }
}

const foo = validateRFC("hello 0670 world");
console.log(foo);

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

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