首页 > 解决方案 > Regex don't work in reactive form control

问题描述

I have the following regex for URL that require to start from http or https

(http:|https:)+[^\s]+[\w] that work very good for what I want (https://regexr.com/598hs)

But when I try to run it in angular validation, it doesn't work.

    this.form = fb.group({
      numberCode: ['', Validators.pattern(`(http:|https:)+[^s]+[w]/g`)]
    })

https://stackblitz.com/edit/angular-reactive-forms-validation-lulnas

Why so ?

标签: regexangularangular-reactive-forms

解决方案


您也可以尝试通过以下方式进行匹配:Validators.pattern(/^(http:|https:)+[^\s]+.+.+.+$/)


推荐阅读