Skip to main content

ServiceNow Retrieve a href url from a string using javascript regex

 var str='INFRASTRUCTURE | AVAILABILITY | Host or monitoring unavailable | <h3>OPEN Problem P-21115004 in environment <i>Non Production</i></h3><br><small>Problem detected at: 17:09 (UTC) 26.11.2021</small><hr><b>1 impacted infrastructure component</b><hr><br><div><span>Host</span><br><b><span style=\"color:#dc172a; font-size:120%\">NOAG-OUD-VM01</span></b><br><p style=\"margin-left:1em\"><b><span style=\"font-size:110%\">Host or monitoring unavailable</span></b><br>Host or monitoring unavailable due to connectivity issues or server outage</p></div><hr><p><a target=\"_blank\" href=\"https://xxxxx.live.dynatrace.com/#problems/problemdetails;pid=-111260345472460xxxx_16379465660xxxx\">Open in Browser</a></p>';



var patt = /<a[^>]*href=["']([^"']*)["']/g;
while(match=patt.exec(str)){
  gs.print(match[1]);
}

*** Script: https://xxxx.live.dynatrace.com/#problems/problemdetails;pid=-111260345472460xxxx_16379465660xxxx
 
 
 
(ServiceNow ) 

 

Comments

  1. The article provides a practical example of using JavaScript regular expressions in ServiceNow to extract an href URL from an HTML string. The use of RegExp.exec() with a loop makes the approach useful for processing content that may contain multiple anchor elements.

    The regular expression clearly demonstrates how the href attribute can be identified and the URL captured from the surrounding HTML content. This is a useful example for developers working with string manipulation and pattern matching, and the fundamentals can be explored further through a Javascript Course in Chennai.

    ReplyDelete
  2. The example also shows how a relatively small JavaScript pattern can address a specific URL-extraction requirement within ServiceNow scripting. Understanding these techniques can be useful for developers who want to strengthen their practical JavaScript skills through Javascript Training in Chennai.

    ReplyDelete
  3. Overall, the article offers a concise and practical solution for extracting URLs from HTML strings. Similar JavaScript and web-development techniques can be useful when building applications and working on Web Development Final Year Projects.

    ReplyDelete

Post a Comment

Popular posts from this blog

ServiceNow timeout settings for virtual agent chat

  After connecting to live agent User is connected to live agent and not responding to live agent chat or distracted. The below properties controls the timeout settings, the OOB settings for reminder is [180 sec] and for cancelling the chat is [360 sec]. The job is default configured to 2 min so I believe no tweaking is required here.  Property -   com.glide.cs.idle_chat_reminder_timeout                  com.glide.cs.idle_chat_cancel_timeout Scheduled job   - Idle Chat Timer Task   https://community.servicenow.com/community?id=community_article&sys_id=1453b03bdbaad0109e691ea668961929   (ServiceNow ) 

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){...