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

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

Bhavya11
Kilo Patron

Hi @Srvs 

 

Please try below code 

 

 

var data = "test comments [Click Hete|http://example.com]";
var matches = data.match(/^(.*?)\s*\[(.*?)\|(.*?)\]$/);

if (matches) {
    var part1 = matches[1]; 
    var part2 = matches[2]; 
    var part3 = matches[3];

    gs.print(part1);
    gs.print(part2);
    gs.print(part3);
} else {
    gs.print("No match found");
}

 

 

 

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

 

 

Thanks

BK

Hi Bhavya,

Thank you for prompt response. we need to put closed square braket "]"

in above code data field contains dynamic comments from user. i have converted your code like below, 

its not working, please suggest me if did anything wrong.

input: test [link title|http://example.com]
 
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+part2+'[code]<a href="'+'"'+part3+'"'+'target="_blank">'+'LINK'+'</a>[/code]';
        }
        else {
            //gs.print("No match found");
        }
 
 

Hi @Bhavya11 ,

if i retun like below,

return part1+'[code]<a href="'+'"'+part3+'"'+'target="_blank">'+'LINK'+'</a>[/code]';

putput is coming like: testhttp://example.com][/code"target="_blank">LINK

expected is test http://example.com 'target="_blank">'+'LINK'+'</a>[/code]

Bhavya11
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