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

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

问题描述

在下面的代码中,我传递了一个包含密码的FormControl.我希望当密码为aA1[11]时,RegExp.test方法应返回false,但返回true!为什么我的代码返回null而不是错误对象{ 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' }

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' }

此前向查找是否应该不匹配(?=.*[!@#$%^&*()_+}{":'?&gt.<,])

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&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天全站免登陆