- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-29-2024 08:48 AM
Hello team,
Im not good at all at regex.
If you run the code below in BG script, you will get this result.
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);
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-29-2024 09:20 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-29-2024 09:20 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-29-2024 10:13 AM
Thank you!
Works great.
Will learn from that one.
have a nice day
/Petr