简体   繁体   中英

Regular expression to validate FQDN

I am trying to use following Regular Expression to validate FQDN in Javascript. This works fine in C#. But Page throws 403 error when used in javascript. 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.

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. For example, the optional dot makes the regex susceptible to catastrophic backtracking . This is a good way to crash your browser .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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