Recently I was asked to figure out a way to convert a web address into a fully qualified domain name using regex. The idea was to have a user type in the base instance or server name for an integration. I’ll walk through an example of how to do that in this post.

Add a New Field

The first this I did was add a URL field to the table that needed to contain the endpoint.

Create a Business Rule

The next thing I did was create a Business Rule that runs whenever the endpoint changes. I wanted the Business Rule to convert a URL into a fully qualified domain name, like the examples below.

https://www.myserver.mycompany.com/    –    www.myserver.mycompany.com

http://myserver.mycompany.com    –    myserver.mycompany.com

www.myserver.mycompany.com/    –    www.myserver.mycompany.com

myserver.mycompany.com/login    –    myserver.mycompany.com

 

Here is the script I used:

1
(function executeRule(current, previous /*null when async*/) {

var endpoint = current.web_endpoint.toString();
endpoint = endpoint.match(/^(?:http(?:s)?\:\/\/)?(.*[^/])\/?.*$/i)[1];
endpoint = encodeURI(endpoint);
current.web_endpoint = endpoint;

})(current, previous);

*Note: I had to put [1] after my regex to access the first group since that’s what contained the correct endpoint*

Final Result

Once the business rule was applied to the field, I had the following result: