Question

I am using a regex expression to validate a domain name (my question here is not to determine if the regex is good or not!).

Here is the regex I am using: ^(?:[a-zA-Z0-9]+(?:-*[a-zA-Z0-9])*.)+[a-zA-Z]{2,6}$

Using this regex in my C# back-end, the domain name org.comabcd is flagged as invalid (because there are 7 caracters in the domain name extension). So this is great!

Using this regex in an online javascript regex validator (http://regexpal.com/), I found the same behavior. This is still great!!

But, my UI front-end use this regex in a Knockout JS custom validation on a custom DataAnnotationsModelValidator and the domain name is flagged as valid.

Here is the code I am using:

ko.validation.rules["domainname"] = {
    validator: (val: any, validate: boolean) => {
         var regex = new RegExp("^(?:[a-zA-Z0-9]+(?:\-*[a-zA-Z0-9])*\.)+[a-zA-Z]{2,6}$");
        return regex.test(val);
    },
message: Localization.validation_domain_name_invalid_format
};

I know the validator works "a little bit" because when trying to have digits in the domain name extension, it is correctly flag as invalid.

Any idea what is going on, why the regex seems to be not use correctly?

Was it helpful?

Solution

If you know what you are using as your regex at compile time, and not constructing it on the fly then you should use a regex literal:

var regex = /^(?:[a-zA-Z0-9]+(?:\-*[a-zA-Z0-9])*\.)+[a-zA-Z]{2,6}$/

Literals are compiled at the loading of the script, whereas RegExp is changeable at runtime so is slightly slower in performance.

As to why it isn't working correctly, it is probably down to not properly escaping your string. When using regex as a string in RegExp, you have to escape any backslashes, so changin the expression to ^(?:[a-zA-Z0-9]+(?:\\-*[a-zA-Z0-9])*\\.)+[a-zA-Z]{2,6}$ should work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top