简体   繁体   English

错误类型错误:无法读取 null 的属性“1”

[英]ERROR TypeError: Cannot read property '1' of null

I am trying to parse card data that is passed through a card reader, however, I am getting an error in regards to the regex.我正在尝试解析通过读卡器传递的卡数据,但是,我收到了有关正则表达式的错误。 I understand it means that this.match is null but I am not sure why it is null.我明白这意味着 this.match 为空,但我不确定为什么它为空。 Any suggestions?有什么建议? (Using Angular 9) (使用 Angular 9)

    CardData: string;
    CardPattern = new RegExp("^\%B(\d+)\^(\w+)\/(\w+)\^\d+\?;\d+=(\d\d)(\d\d)\d+$");
    CardNumber: string;
    FirstName: string;
    LastName: string;
    ExpData: string;
    match: RegExpExecArray;

    constructor(private snackbar: SnackBar) {
    }

    ngOnInit() {
        // getaCardData();
        this.CardData = "%B6545461234613451^DOE/JOHN^00000000000000000000000?;6545461234613451=984651465116111?"

        this.match = this.CardPattern.exec(this.CardData);

        this.CardNumber = this.match[1]
        this.FirstName = this.match[3];
        this.LastName = this.match[2];
        this.ExpData = this.match[5] + "/" +this.match[4];

        console.log(this.CardNumber);
    }

Your pattern does not match because you have an additional character ?您的模式不匹配,因为您有一个额外的字符 at the end of your string: this.CardData = %B6545461234613451^DOE/JOHN^00000000000000000000000?;6545461234613451=984651465116111?"在字符串的末尾: this.CardData = %B6545461234613451^DOE/JOHN^00000000000000000000000?;6545461234613451=984651465116111?"

Replace it with:替换为:

this.CardData = "%B6545461234613451^DOE/JOHN^00000000000000000000000?;6545461234613451=984651465116111"

or change your pattern instead with: ^\\%B(\\d+)\\^(\\w+)\\/(\\w+)\\^\\d+\\?;\\d+=(\\d\\d)(\\d\\d)\\d+\\?$或更改您的模式: ^\\%B(\\d+)\\^(\\w+)\\/(\\w+)\\^\\d+\\?;\\d+=(\\d\\d)(\\d\\d)\\d+\\?$

Update: Added working code snippet for you reference!更新:添加了工作代码片段供您参考! Try adding flags and it should work.尝试添加标志,它应该可以工作。

 const regex = new RegExp(/^\\%B(\\d+)\\^(\\w+)\\/(\\w+)\\^\\d+\\?;\\d+=(\\d\\d)(\\d\\d)\\d+\\?$/, 'gm'); const input = `%B6545461234613451^DOE/JOHN^00000000000000000000000?;6545461234613451=984651465116111?`; var match = regex.exec(input); document.write(match.length);

It seems that it has something to do with escape character.似乎与转义字符有关。 I have tried on Chrome Developer tools running the following two lines.我已经尝试过运行以下两行的 Chrome 开发者工具。

1. Without Template Literal
new RegExp("^\%B(\d+)\^(\w+)\/(\w+)\^\d+\?;\d+=(\d\d)(\d\d)\d+$").exec('%B6545461234613451^DOE/JOHN^00000000000000000000000?;6545461234613451=984651465116111?')

result: null

2. With Template Literal
new RegExp($`{"^\%B(\d+)\^(\w+)\/(\w+)\^\d+\?;\d+=(\d\d)(\d\d)\d+$"}`).exec('%B6545461234613451^DOE/JOHN^00000000000000000000000?;6545461234613451=984651465116111?')

result: ["O", index: 20, input: "%B6545461234613451^DOE/JOHN^00000000000000000000000?;6545461234613451=984651465116111?", groups: undefined]

Wrapping your Regex expression in a Template Literal should solve the problem.将您的 Regex 表达式包装在模板文字中应该可以解决问题。 The compile process has likely took the wild card characters into account during the build, which suggests why it works after compilation.编译过程可能在构建过程中考虑了通配符,这说明了为什么它在编译后有效。

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

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