JavaScript regular expression validation for domain name?

后端 未结 6 1233
野的像风
野的像风 2020-12-17 23:45

How to check a valid domain name and username with regular expression in JavaScript?

function validate()
{
    var         


        
6条回答
  •  再見小時候
    2020-12-18 00:22

    Don't mix up the RegExp constructor with regex literals. Use either

    /^[a-zA-Z0-9._-]+\\[a-zA-Z0-9.-]$/
    

    or

    new RegExp("^[a-zA-Z0-9._-]+\\\\[a-zA-Z0-9.-]$");
    

    Not sure what the backslash does in there, btw. Did you want to match a dot? In literal, use \., in string use \\..

提交回复
热议问题