[英]Regular expression to validate FQDN
I am trying to use following Regular Expression to validate FQDN in Javascript. 我正在尝试使用以下正则表达式来验证Javascript中的FQDN。 This works fine in C#.
在C#中可以正常工作。 But Page throws 403 error when used in javascript.
但是在JavaScript中使用Page时会引发403错误。 What am i doing wrong?
我究竟做错了什么?
if (!fqdn.match(/(?=^.{1,254}$)(^(?:(?!\d|-)[a-zA-Z0-9\-]{1,63}(?<!-)\.?)+(?:[a-zA-Z]{2,})$)/))
{
alert("Not a valid FQDN");
return false;
}
Your regex uses a negative lookbehind assertion (?<!-)
, and those aren't supported by JavaScript. 您的正则表达式使用否定的后向断言
(?<!-)
,而JavaScript不支持它们。
You can rewrite your regex to match exactly the same way without lookbehinds: 您可以重写正则表达式,以完全相同的方式进行匹配,而无需担心:
/(?=^.{1,254}$)(^(?:(?!\d|-)[a-z0-9-]{0,62}[a-z0-9]\.?)+(?:[a-z]{2,})$)/i
I'm not so sure if the logic behind the regex really is correct for validating a FQDN, though, but that's a different problem. 不过,我不确定正则表达式背后的逻辑是否真的适用于验证FQDN,但这是另一个问题。 For example, the optional dot makes the regex susceptible to catastrophic backtracking .
例如,可选点使正则表达式易于遭受灾难性的回溯 。 This is a good way to crash your browser .
这是使浏览器崩溃的好方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.