jqunit,测试正则表达式在某些时候失败,为什么? [英] jqunit, testing regex fails at some point, why?

查看:23
本文介绍了jqunit,测试正则表达式在某些时候失败,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 jqunit 的帮助下处理关于正则表达式的测试用例:

I am working on testcases regarding reular expression with the assisstance of jqunit:

test("Date entry should be a Date", function () {
    ok(/(\d+[-/\.]\d+[-/\.]\d+)/g.test("10.12.2014"), "Date is not parsed correctly: 10.12.2014");
    ok(/(\d+[-/\.]\d+[-/\.]\d+)/g.test("10-12-2014"), "Date is not parsed correctly: 10-12-2014");
    ok(/(\d+[-/\.]\d+[-/\.]\d+)/g.test("10/12/2014"), "Date is not parsed correctly: 10/12/2014");
});

第二个条件失败,但在浏览器的控制台或 regexr.com 上执行它们所有这些都是真的.这种行为的来源有什么提示吗?

The second condition is failing, but executing them in the browser's console or on regexr.com all of them a true. Any hints where this behaviour comes from?

推荐答案

当你只使用 test 时,不要在正则表达式中使用 g 标志:它没有没有意义,更重要的是,它使它成为一个迭代器,其结果随着每次对 test 的调用而改变,我想文字正则表达式在 jqunit 中使用了不止一次.

Don't use the g flag in a regex when you only use test : it doesn't make sense and, more importantly, it makes it an iterator whose result changes with each call to test and I guess the literal regexes are used more than once in jqunit.

改变

/(\d+[-/\.]\d+[-/\.]\d+)/g

/(\d+[-/\.]\d+[-/\.]\d+)/

这篇关于jqunit,测试正则表达式在某些时候失败,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆