RegExp.test 没有给出正确的结果 [英] RegExp.test not giving correct result

查看:27
本文介绍了RegExp.test 没有给出正确的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我传递了一个包含密码的 FormControl.我希望当密码是 aA1[11] 时,RegExp.test 方法应该返回 false 但它返回 true!为什么我的代码返回 null 而不是错误对象 {验证密码:{有效:假,message: '密码必须包含 1 个小写字母 [a-z]、1 个大写字母 [A-Z]、1 个数字 [0-9]、1 个特殊字符,长度应在 6-10 个字符之间'}

In the following code, I pass a FormControl which contains a password. I expect that when the password is aA1[11], the RegExp.test method should return false but it returns true! Why my code is returning null instead of error object { validatePassword: { valid: false, message: 'password must contain 1 small-case letter [a-z], 1 capital letter [A-Z], 1 digit[0-9], 1 special character and the length should be between 6-10 characters' }

这个前向查找不应该使匹配失败(?=.*[!@#$%^&*()_+}{":'?>.<,])

Shouldn't this forward look up fail the match (?=.*[!@#$%^&*()_+}{":'?&gt.<,])

  validatePassword(control: FormControl) {

    let password: string = control.value;
 /*    So the rule for password is
     6-10 length
     contains a digit
     contains a lower case alphabet
     contains an upper case alphabet
     contains one more special character from the list !@#$%^&*()_+}{":;'?/>.<,
     does not contain space
     */
    let REG_EXP = new RegExp('(?=^.{6,10}$)(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+}{":\'?&gt.<,])(?!.*\\s).*$');
    /*RegExp's test method returns true if it finds a match, otherwise it returns false*/
    console.log('password: ',password);
    console.log('test result ',(REG_EXP.test(password)));
    return (REG_EXP.test(password)) ? null : {
      validatePassword: { //check the class ShowErrorsComponent to see how validatePassword is used.
        valid: false,
        message: 'password must contain 1 small-case letter [a-z], 1 capital letter [A-Z], 1 digit[0-9], 1 special character and the length should be between 6-10 characters'
      }
    }
  }

我从 Karma 测试用例调用上述函数

I am calling the above function from my Karma test case

fit('A password of length between 6-10 characters and containing at least 1 digit, at least  1 lowercase letter,  at least 1 upper case ' +
    'letter and but NOT at least 1 special character from the list !@#$%^&*()_+}{":;\'?/>.<, shall NOT be accepted',
    inject([HttpClient,HttpTestingController],(httpClient:HttpClient)=>{
      let helper = new HelperService(loaderService,httpClient);
      let passwordField = new FormControl();
      let password = 'aA1[11]';
      passwordField.setValue(password);
      let result = helper.validatePassword(passwordField);
      expect(result).toEqual(expectedErrorResponse);
    }));

我在控制台看到的输出是

The output I see in the console is

password:  aA1[11]
test result  true

推荐答案

问题在于正则表达式.我将 &amp 改为 &&quot"&gt>&lt<. 似乎在我的代码(角度)中,& 是按字面处理的.所以 aA1[11] 中的 a 中的 a 匹配&amp

The issue was with regex. I changed &amp to just & and &quot to " and &gt to > and &lt to <. It seems that in my code (angular), the letters after & were being treated literally. so the a in aA1[11] was matched with a in &amp

这篇关于RegExp.test 没有给出正确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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