意外的javascript行为问题 [英] Unexpected javascript behaviour issue

查看:116
本文介绍了意外的javascript行为问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这个验证方法但是遇到了问题。

I wrote this validation method but am having issues with it.

function validate_password(pwd)
{
    var score = 0;

    // Min length is 8
    if (pwd.length<8)
        return false;

    // Is lower present?
    if (/[a-z]/g.test(pwd))
    {
        console.log('a-z test on "'+pwd+'":' + /[a-z]+/g.test(pwd));
        score++;
    }

    // Is upper present?
    if (/[A-Z]/g.test(pwd))
    {
        console.log('A-Z test on: "'+pwd+'":' + /[A-Z]+/g.test(pwd));
        score++;
    }

    // Is digit present?
    if (/\d/g.test(pwd))
    {
        console.log('digit test on: "'+pwd+'":' + /\d/g.test(pwd));
        score++;
    }

    // Is special char present?
    if (/\W/g.test(pwd))
    {
        console.log('spec char test on: "'+pwd+'":' + /\W/g.test(pwd));
        score++;
    }

    if (score>=3)
        return true;
    else
        return false;
}

这是写入控制台的内容:

This is what is written to the console:

>>> validate_password('aaasdfF#3s')
a-z test on "aaasdfF#3s":true
A-Z test on: "aaasdfF#3s":true
digit test on: "aaasdfF#3s":true
spec char test on: "aaasdfF#3s":true
true

>>> validate_password('aaasdfF#3s')
a-z test on "aaasdfF#3s":true
false

在第一次尝试时,它似乎按预期工作,但是当我第二次调用该方法时,它无法按预期工作。

On the first try it seems to work as expected but when I call the method a 2nd time, it doesn't work as expected.

所以,我的问题是为什么第一次尝试和第二次尝试的结果之间存在差异?

So, my question is why are there differences between the results from the first try and the 2nd try?

谢谢! :)

推荐答案

请参阅 test


当你想知道字符串中是否找到模式时,请使用test方法(类似于String.search方法);有关更多信息(但执行速度较慢),请使用exec方法(类似于String.match方法)。与exec一样,在同一个正则表达式实例上多次调用的测试将超过上一个匹配

解决方案是从正则表达式中删除全局或 g 标记:

The solution is to remove the global or g flag from your regexes:

/ [az] / 而不是 / [az] / g ,依此类推。

考虑这个简单示例,看看问题存在的原因:

Consider this simple example to see why the problem exists:

var l = /[a-z]/g;

// initial search starts at the beginning, matches "a" and returns true
l.test("a"); // true
// since the first character matched, lastIndex moves to the next index - 1
l.lastIndex; // 1

// this time we pass a different string to the regex, but unfortunatly it
// starts searching from the lastIndex which is 1. There are no lower case
// letters from this point onwards (only "---"), so return value is false.
l.test("x---"); // false
// Since this search failed, lastIndex wraps around to the beginning, so the 
// next search will work as expected
l.lastIndex; // 0

对于您的给定输入aaasdfF#3s,小写 [az] 测试将成功7次,因为有7个小写字符,但第8次失败。并且从第9次到第15次再次成功,依此类推。其他测试将在每个替换时间失败,因为每种类型的角色中只有一个 - F3,当测试失败时,它将 lastIndex 换行为0。

For your given input "aaasdfF#3s", the lower case [a-z] test would have succeeded 7 times as there are 7 lower case characters, but fail the 8th time. And again succeed from the 9th to 15th time, and so on. The other tests will fail every alternate time as there is only one of each type of character - "F", "#", and "3" and it wraps around lastIndex to 0 when the test fails.

这个问题似乎源于这样一个事实:状态在函数调用之间保留在那些RegExp对象中,这意味着一个RegExp对象只创建一次,而不是每次调用该函数。这个小小的测试证实了这一点:

The problem seems to be stemming from the fact that state is preserved in those RegExp objects between function calls, meaning a RegExp object is only created once, and not every time the function is called. This little test confirms it:

function RegExpStatePersistenceTest() {
    var regex = /[a-z]/g;

    regex.counter = regex.counter || 0;
    regex.counter++;

    console.log("counter:" + regex.counter);
}

RegExpStatePersistenceTest(); // counter: 1
RegExpStatePersistenceTest(); // counter: 2
RegExpStatePersistenceTest(); // counter: 3
RegExpStatePersistenceTest();​ // counter: 4

如果a使用 new RegExp(..)显式创建了新对象,然后在每次调用函数时,将创建一个全新的RegExp对象,并且不会保留状态电话。

If a new object was explicitly created using new RegExp(..), then on each invocation of the function, a fresh new RegExp object will be created, and state will not be preserved between calls.

另见为什么-regexp-with-global-flag-in-javascript-give-wrong-results

这篇关于意外的javascript行为问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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