【问题标题】:How to check if a given value contains an exclusion prefix?如何检查给定值是否包含排除前缀?
【发布时间】:2022-12-11 14:52:10
【问题描述】:

如何检查用户输入值的前缀是什么,如果是“ABC”则不验证。

这意味着不应在条件中检查包含前缀“ABC”的值,而应将其重定向。

所以在代码中应该是这样的:用户输入的内容,i.n.这产品, 应该检查它是什么前缀,如果它是“ABC”,我们不验证它,只发生重定向。

字符已经设置为大写,因此无需转换它们的大小。

而在这个 if 中必须包含 (!isAvaliable(this.product)) 即 isAvaliable(boolean) 函数检查产品是否存在。我想在这个 if 中进行设置,以便在产品可用或以“ABC”开头时发生重定向。

check(): void {
    if (this.product) {
        if (!isAvaliable(this.product)) {
            const error = 'not found!';
            this.toastrService.error(error);
            return;
        }
        this.findProduct.redirect(this.product);
    }
}

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    您可以使用regex 来查找字符串中的模式。

    isInvalid(product: string) {
        const regex = new RegExp('ABCw*');
        return regex.test(product);
      }
    
      check(): void {
        const validStr = 'test5792';
        const invalidStr = 'ABCtest5792';
        console.log(this.isInvalid(validStr));  // false
        console.log(this.isInvalid(invalidStr));  // true
      }
    

    RegExp.prototype.test

    【讨论】:

      猜你喜欢
      • 2015-05-23
      • 2023-02-01
      • 1970-01-01
      • 2020-05-24
      • 2015-06-24
      • 1970-01-01
      • 2013-04-22
      • 1970-01-01
      • 2011-06-03
      相关资源
      最近更新 更多