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.

How to extract from tags

servicenow14710
Tera Expert

Hello developers, 

 

I have an implementation where i get api 400 error and it is returning response in this format:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
</body></html>

 

 

From the above response i need to extract what is inside <p> tags. Any help is appreciated, Thanks!

 

 

 

1 ACCEPTED SOLUTION

KrishanSharma
Tera Expert

Hello,

 

You can extract URL from the <p> tag as follows:

 

var str = "YOUR_RESPONSE";
var url = str.substring(str.indexOf('<p>')+3, str.indexOf('</p>'));

 

 

Hope it helps 🙂

View solution in original post

3 REPLIES 3

KrishanSharma
Tera Expert

Hello,

 

You can extract URL from the <p> tag as follows:

 

var str = "YOUR_RESPONSE";
var url = str.substring(str.indexOf('<p>')+3, str.indexOf('</p>'));

 

 

Hope it helps 🙂

Harish Murikina
Tera Guru

Hi @servicenow14710 

You can get the value on client side something like below, not sure if you are looking to  get on server or client.

 

<!DOCTYPE html>
<html><head>
<script>

function getPtag() {
alert(document.getElementsByTagName("P")[0].innerHTML)
}

</script>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
<button onClick="getPtag()" name="GET TAG">GET TAG</button>

</body></html>

 

HarishMurikina_0-1732099525248.png


Regards,
Harish Murikinati.

GlideFather
Tera Patron
Try something like the code below:

var
startTag = '<p>';
var endTag = '</p>';
var startIndex = htmlContent.indexOf(startTag) + startTag.length;
var endIndex = htmlContent.indexOf(endTag);

if (startIndex > -1 && endIndex > -1 && startIndex < endIndex) {
var extractedText = htmlContent.substring(startIndex, endIndex).trim();
gs.print("Extracted Text: " + extractedText);
}
———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */