Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Regular expression to include square braces

Srvs
Kilo Guru

Hi Team,

 

i need to split the below sentence using regex, Please help me out in 2nd line of the code.

i want to include the symbols [,],| as delimiters. please suggest me.

 

var data = "test comments [link title|http://example.com]"
var items = data.split(/[,|;]/);
var numSel = items.length;
 for (var i = 0; i < numSel; i++)
 {

 }
 gs.print(items[0])
 gs.print(items[1])
 gs.print(items[2])
 
output needed is: 
test comments
link title
 
regards,
Srvs.
1 ACCEPTED SOLUTION

Bhavya11
Kilo Patron
Kilo Patron

hi @Srvs 

 

from your code Assuming urlify function is defined elsewhere

Please try like below

var finComment = 'test [Click Here|http://example.com]';
var htmlComment = finComment;
var sysIdList = htmlComment.match(/^(.*?)\s*\[(.*?)\|(.*?)\]$/);

if (sysIdList) {
    var part1 = sysIdList[1];
    var part2 = sysIdList[2];
    var part3 = sysIdList[3];
    var htmlOutput = part1 + '[code]<a href="' + part3 + '" target="_blank">' + part2 + '</a>[/code]';
    gs.print(htmlOutput);
} else {
    gs.print("No match found");
}

i am getting output as :

test[code]<a href="http://example.com" target="_blank">Click Here</a>[/code]

 

If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful."

Thanks,

BK

View solution in original post

6 REPLIES 6

Hi @Bhavya11 

its returning exactly in background script,

im trying to execute the  script in flow designer, which will take the input from jira and process for servicenow comments.

 

            var finComment = payloadObj.comment;
            var htmlComment = urlify(finComment);
            var sysIdList = htmlComment.match(/^(.*?)\s*\[(.*?)\|(.*?)\]$/);
            if (sysIdList)
            {
            var part1 = sysIdList[1];
            var part2 = sysIdList[2];
            var part3 = sysIdList[3];
            return part1 + '[code]<a href="' + part3 + '" target="_blank">' + part2 + '</a>[/code]';
        }
        else {
            //gs.print("No match found");
        }
 
input: testing123 [link title|http://example.com]
output getting into comments: testing123http://example.com][/code" target="_blank">link title
expected: testing123http://example.com][/code" target="_blank">link title</a>[/code]
 
i guess after url we have closing square braket ] is causing the issue. please suggest.

Hi @Bhavya11 

i removed the $ symbol from  var sysIdList = htmlComment.match(/^(.*?)\s*\[(.*?)\|(.*?)\]$/);

now its working perfect.,

thanks a lot for your help 🙂