Problem with RegEx in ServiceNow

Community Alums
Not applicable

So I have a requirement to allow URLs, No spaces and comma delimited.  A dash "-" is allowed just not at beginning or beside a period "." And lastly * is only available in the first portion of the URL. I have create a regular expression that passes via regex101, but in my ServiceNow instance it fails.

Pass would be:

www.test.com
www.example.com,www-01.user.com,www.user-test.com,*.example.com
*.example.com,test.user.com

Fail:

www*.example.com
-www.example.com
www-.example.com
www.-example.com

Here is my RegEx:

^(([a-zA-Z0-9-]{1,}[a-zA-Z0-9]{1,150}\.[a-zA-Z0-9]{1,150}[a-zA-Z0-9-]{1,150}\.[a-zA-Z0-9]{1,150})|^([*]{1,1}\.[a-zA-Z0-9]{1,150}[a-zA-Z0-9-]{1,150}\.[a-zA-Z0-9]{1,150}))|((\,[a-zA-Z0-9-]{1,}[a-zA-Z0-9]{1,150}\.[a-zA-Z0-9]{1,150}[a-zA-Z0-9-]{1,150}\.[a-zA-Z0-9]{1,150})|(\,[*]{1,1}\.[a-zA-Z0-9]{1,150}[a-zA-Z0-9-]{1,150}\.[a-zA-Z0-9]{1,150}))

1 ACCEPTED SOLUTION

Community Alums
Not applicable

I got the Client script version to work, the user can enter values in a single field and the array works perfect.  The script you posted the test line was backwards.

 if (!urlArray[x].test(re))

should actually be (!re.test(urlArray[x])) and then it worked perfectly.  Also I mistakenly had a return true from the client script Yousaf provided below.  I also got the - to work in beginning and not near a . and I got only the since * to work.  Here is my final solution.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == '') {
        return;
    }
    var urlArray = [];
    var urlField = g_form.getValue('provide_url');
    urlArray = urlField.split(',');
    // This will restrict the usages of certain special characters. 
    
    var re = /^(([a-zA-Z0-9-]{1,}[a-zA-Z0-9]{1,}\.[a-zA-Z0-9]{1,}[a-zA-Z0-9-]{1,150}\.[a-zA-Z0-9]{1,})|^([*]{1,1}\.[a-zA-Z0-9]{1,}[a-zA-Z0-9-]{1,}\.[a-zA-Z0-9]{1,}))$/;
    
    for (var x = 0; x < urlArray.length; x++) {
        if (!re.test(urlArray[x])) {
            g_form.showFieldMsg('provide_url','Invalid URL entry, please exclude slashes, colons and spaces.','error');
            return false;
        }
    }
}

View solution in original post

15 REPLIES 15

Sasha Sobolev
Mega Guru

Hi Brian,

I modified yours to the below - it worked on all of your test cases via https://regexr.com/ & in ServiceNow. A couple notes..

- Shortened it up assuming you only want to capture something like www.google.com rather than any possible IPs like 10.5.1.5

- Using OR statements (|) to ensure that the first characters are either a * or string of character. Also so that each string only have "-" in the middle and not touching a period

- It looks like you are using this on a specific field given the "^" at the beginning. If you want to expand to watch all urls within a text, you can just add "g" after the closing "/" on the regex and take off the "^" at the beginning

^(([a-zA-Z0-9]+([a-zA-Z0-9-]*[a-zA-Z0-9]+|[a-zA-Z0-9]*)|\*)\.[a-zA-Z0-9]+([a-zA-Z0-9-]*[a-zA-Z0-9]+|[a-zA-Z0-9]*)\.[a-zA-Z0-9]+([a-zA-Z0-9-]*[a-zA-Z0-9]+|[a-zA-Z0-9]*))

Hope that helps,

Sasha

Community Alums
Not applicable

I input:

www.user.com,www-www.text.com,www.w-user.com,*.example.com

I got invalid message. I have regex report Invalid URL.

Brian, are you putting in that whole string? If you're using variable validation, you will have to ask them to put in one at a time. If you want to check multiple you'll have to use a catalog client script and do some scripting.

You could also use a multi row variable set and have them "add" multiple URLs - in that case you can still keep the field validation.

-Sasha

Below is example of client script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

     var re = /^(([a-zA-Z0-9]+([a-zA-Z0-9-]*[a-zA-Z0-9]+|[a-zA-Z0-9]*)|\*)\.[a-zA-Z0-9]+([a-zA-Z0-9-]*[a-zA-Z0-9]+|[a-zA-Z0-9]*)\.[a-zA-Z0-9]+([a-zA-Z0-9-]*[a-zA-Z0-9]+|[a-zA-Z0-9]*))/;

    var urlField = g_form.getValue('url_field');
    urlArray = urlField.split(',');
    
    for (var x=0; x<urlArray.length; x++) {
       if (!urlArray[x].test(re)) {
          g_form.addErrorMessage('regex failed');
          g_form.clearValue('url_field');
          return false;
    }
}

Community Alums
Not applicable

Just trying to use the single variable. I don't seem to be getting the proper failure message, even the single URL isn't returning a failure message.

Brian