Regular Expression to validate Domain Name

Mantautas
Tera Contributor

Dear Community,

 

On the Service catalog form we ask users for provide FQDM/Fully Qualified Domain name (www.example.com, www.example.co.uk, etc). ServiceNow has out-of-the box Regular expression for URL, however it contains "http".  Which regular expression should be used to check user provided URL in FQDM format only, no additional characters allowed. Thanks!

 

Regards,
Mantautas.

2 ACCEPTED SOLUTIONS

Valid point! 🙂 Try this:

/^(?!:\/\/)(w{3}+\.)?[a-zA-Z0-9][a-zA-Z0-9-]+\.[a-zA-Z]{2,6}?$/ji

View solution in original post

Perhaps this minor change at the end would do:

^(?!:\/\/)(w{3}+\.)?[a-zA-Z0-9][a-zA-Z0-9-]+\.[a-zA-Z\.]{2,6}?$

View solution in original post

6 REPLIES 6

Laszlo Balla
Mega Sage
Mega Sage

There's plenty of examples online, but this worked fine for me:

/^(?!:\/\/)([a-zA-Z0-9]+\.)?[a-zA-Z0-9][a-zA-Z0-9-]+\.[a-zA-Z]{2,6}?$/i

 

Here's a test:

 

var r = new RegExp('^(?!:\/\/)([a-zA-Z0-9]+\.)?[a-zA-Z0-9][a-zA-Z0-9-]+\.[a-zA-Z]{2,6}?$', 'ji');
var testString = 'www.testpage.com';
gs.info(testString.match(r) ? 'match' : 'does not match');

Forgot to mention, the 'j' flag is only added to tell the engine to treat this expression as a Java expression. It can be dismissed.

Thanks a lot Laszlo!

This expression would allow to put any letters on "www" part. 

For example zzz.testpage.com..Also more that 3 letters would be accepted, for example "wwww.testpage.com". How do i control the "www" piece, so its only 3 letters allowed and only 3W 🙂

Valid point! 🙂 Try this:

/^(?!:\/\/)(w{3}+\.)?[a-zA-Z0-9][a-zA-Z0-9-]+\.[a-zA-Z]{2,6}?$/ji