The CreatorCon Call for Content is officially open! Get started here.

Regex question

Petr Pastuszek1
Tera Contributor

Hello team,

Im not good at all at regex.

If you run the code below in BG script, you will get this result.

 

PetrPastuszek1_0-1709224899626.png

 

I need regex not based on image html tag because sometimes it comes as image, sometimes as something else, but need it based on string --> /rest/api/3/attachment/content/

 

Input

"<p>Tets text</p>\n\n<p><span class=\"image-wrap\" style=\"\"><img src=\"/rest/api/3/attachment/content/999999999\" height=\"298\" width=\"293\" style=\"border: 0px solid black\" /></span></p>"

 

Expected output

 

 

/rest/api/3/attachment/content/999999999
999999999

 

Could you help me to change the below regex variable 'match' in a way that I will be able to achieve above requirement?

 

Run the code below to see what it prints:

 

decodedData = "<p>Tets text</p>\n\n<p><span class=\"image-wrap\" style=\"\"><img src=\"/rest/api/3/attachment/content/999999999\" height=\"298\" width=\"293\" style=\"border: 0px solid black\" /></span></p>"

 
       var match = decodedData.match(/<img.*?src="(.*?)"/g);
        for (var i in match) {
		
            var attachmentsource = match[i].split('"')[1]
			var attachmentID = attachmentsource.split("/").slice(-1);
            gs.print("match[i]: " +  match[i]);
            gs.print(attachmentsource);
            gs.print(attachmentID);

        }

 

1 ACCEPTED SOLUTION

Ivan Betev
Mega Sage
Mega Sage

Hi @Petr Pastuszek1 ,

Try something like this:

var regex = /\/rest\/api\/3\/attachment\/content\/(\d+)/;
var str = '<p>Tets text</p>\n\n<p><span class=\"image-wrap\" style=\"\"><img src=\"/rest/api/3/attachment/content/999999999\" height=\"298\" width=\"293\" style=\"border: 0px solid black\" /></span></p>';
var match = str.match(regex);
if (match) {    
    gs.info(match[0]); 
    gs.info(match[1]);
}

 

Regards, Ivan

View solution in original post

2 REPLIES 2

Ivan Betev
Mega Sage
Mega Sage

Hi @Petr Pastuszek1 ,

Try something like this:

var regex = /\/rest\/api\/3\/attachment\/content\/(\d+)/;
var str = '<p>Tets text</p>\n\n<p><span class=\"image-wrap\" style=\"\"><img src=\"/rest/api/3/attachment/content/999999999\" height=\"298\" width=\"293\" style=\"border: 0px solid black\" /></span></p>';
var match = str.match(regex);
if (match) {    
    gs.info(match[0]); 
    gs.info(match[1]);
}

 

Regards, Ivan

Thank you!

Works great.

Will learn from that one.

 

have a nice day

 

/Petr