Parse Description field or multi-text field using Regex

Brett14
Giga Expert

All, I would like to parse text that is within the description field.   I have used regex before but I am a little stuck on how to parse this out.

My string is below and what is in bold should be removed.   I only want the words 'long description' to display in this field.   Is this possible?

NEW_CALL_REF:7fe4cf7bdb53cb404ad038f0ad961926 New Call DESC: Long Description NEW CALL SHORT_DESC: Short Description

I created a business rule to run on Before and I have so far is below.   Any help???

// Add your code here

var descrip = current.getValue('description');

var regex = /New Call DESC:.*/g;

var str = descrip; // Example `NEW_CALL_REF:7fe4cf7bdb53cb404ad038f0ad961926 New Call DESC: Long Description NEW CALL SHORT_DESC: Short Description`;

1 ACCEPTED SOLUTION

Here is an example I was doing via background script:


var string = "NEW_CALL_REF:7fe4cf7bdb53cb404ad038f0ad961926 New Call DESC: Long Description NEW CALL SHORT_DESC: Short Description";



var key1 = "New Call DESC:";


var key2 = "NEW CALL SHORT_DESC:";



var key1Index = string.indexOf(key1) + key1.length;


var key2Index = string.indexOf(key2);



var desc = string.substring(key1Index, key2Index).trim();



gs.print("'" + desc + "'");



Output is:


*** Script: 'Long Description'



I had single quotes on it to make sure the trim() function removed leading and trailing spaces.


View solution in original post

4 REPLIES 4

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

I assume this is coming from an external source?   Do you have any opportunity to edit what comes over?   Parsing is very difficult unless there are standards in place.   For example I see NEW and New and NEW_CALL_REF (underscores) and New Call DESC (spaces).   It would be ideal if you could put a | or ^ or something between each field or even a line break.



If not you could use the javascript substr function to look for "New Call DESC" and grab that text till "NEW CALL SHORT_DESC" assuming these always are in this order.


Hi Michael,



Yes it will alway come in that way into the description.   One being the obvious that it will be different text, but definitely the same order.



I was following around and was able to get this much taken off by



var indx2 = full_text.indexOf('New Call DESC');


var desc = full_text.substring(indx2,full_text.length);



current.description = desc;



my output is:



New Call DESC: Long Description NEW CALL SHORT_DESC: Short Description



I am getting close, but how do I get the above removed


Here is an example I was doing via background script:


var string = "NEW_CALL_REF:7fe4cf7bdb53cb404ad038f0ad961926 New Call DESC: Long Description NEW CALL SHORT_DESC: Short Description";



var key1 = "New Call DESC:";


var key2 = "NEW CALL SHORT_DESC:";



var key1Index = string.indexOf(key1) + key1.length;


var key2Index = string.indexOf(key2);



var desc = string.substring(key1Index, key2Index).trim();



gs.print("'" + desc + "'");



Output is:


*** Script: 'Long Description'



I had single quotes on it to make sure the trim() function removed leading and trailing spaces.


thanks michael



It worked.   I have marked your answer as correct for others.