gpt4 book ai didi

Regex works, but not in Angular Form [duplicate](正则表达式工作正常,但不是角形[复制])

转载 作者:bug小助手 更新时间:2023-10-28 09:35:06 28 4
gpt4 key购买 nike




I need to use this regex for url validation. It works in regex101.com, but it fails validation in my Angular field. I don't know how to modify it in a way that Angular accepts.

我需要使用此正则表达式进行URL验证。它可以在regex101.com中运行,但在我的角度域中没有通过验证。我不知道如何以角度可以接受的方式修改它。


regex: https:\/\/[A-Za-z0-9_.~\/?%#!$&*+,;\[\]\(\)-]+
url: https://something.test.net/a-bc/x_y.json?ab=2021-08-11T

正则表达式:HTTPS:\/\/[A-ZA-Z0-9_.~\/?%#!$&*+,;\[\]\(\)-]+URL:https://something.test.net/a-bc/x_y.json?ab=2021-08-11T


I'm using Validators.pattern() in the form for the field validation.

我在表单中使用Validators.pattern()进行字段验证。


I escaped the slashes with backslashes like shown above, but it still fails validation.

我用如上所示的反斜杠转义了斜杠,但它仍然没有通过验证。


更多回答
优秀答案推荐

In Angular, when using the Validators.pattern() method for form field validation, you don't need to escape the slashes with backslashes in your regular expression.

在ANGLE中,当使用Validators.Pattern()方法进行表单域验证时,您不需要在正则表达式中使用反斜杠转义斜杠。


In the code above, I removed the backslashes before the forward slashes in your regex pattern. It should work correctly with Angular's Validators.pattern() method.

在上面的代码中,我删除了regex模式中正斜杠之前的反斜杠。它应该可以与ANGLE的Validators.Pattern()方法一起正常工作。


Here's the corrected version of your regex pattern without unnecessary backslashes:

以下是您的正则表达式模式的更正版本,没有不必要的反斜杠:


Validators.pattern('https://[A-Za-z0-9_.~/?%#!$&*+,;\\[\\]()-]+')

更多回答

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com