Need to parse string from multiline text

ashu1612
Tera Contributor

I have a requirement where I need to parse a string from multiline text. 

 

For example input string could be

 

Input String will be
"**** Begin bla bla bla-  ****
Hello Word please pick me
from the annoying bla
**** End bla bla bla ****"

 

I need to parse the material in between Begin and End

 

out put should be 

"Hello Word please pick me
from the annoying bla".

 

need some suggestion to perform this in most efficient way. Thanks in advance

1 ACCEPTED SOLUTION

Juhi Poddar
Kilo Patron

Hello @ashu1612 

Here is the script to meet your requirement:

var gr = new GlideRecord('incident');
gr.addQuery('number', 'INC0010683'); // Query the correct record
gr.query();

if (gr.next()) {
    var desc = gr.getValue('description'); // Get the description field value
    gs.print("Input string: " + desc);

    // Encode the input string
    var encodedString = encodeURIComponent(desc);
    gs.print("Encoded string: " + encodedString);
	var arr = encodedString.split('%0A');
	arr.join("");
	var arr2 = arr.slice(1,arr.length-1);
	gs.print( decodeURIComponent(arr2).replaceAll(',',""));
} 
  • To test the script I have considered taking input from description a multiline text field from incident table.
  • This will work for any number of input lines.

Approach:

  • Take the input from multiline text field, encode the entire string.
  • %0A is the encoded value for enter, so split the encoded string wrt %0A and then slice the first and the last line.

Result:

JuhiPoddar_1-1736274315456.png

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

View solution in original post

9 REPLIES 9

Juhi Poddar
Kilo Patron

Hello @ashu1612 

Here is the script to meet your requirement:

var gr = new GlideRecord('incident');
gr.addQuery('number', 'INC0010683'); // Query the correct record
gr.query();

if (gr.next()) {
    var desc = gr.getValue('description'); // Get the description field value
    gs.print("Input string: " + desc);

    // Encode the input string
    var encodedString = encodeURIComponent(desc);
    gs.print("Encoded string: " + encodedString);
	var arr = encodedString.split('%0A');
	arr.join("");
	var arr2 = arr.slice(1,arr.length-1);
	gs.print( decodeURIComponent(arr2).replaceAll(',',""));
} 
  • To test the script I have considered taking input from description a multiline text field from incident table.
  • This will work for any number of input lines.

Approach:

  • Take the input from multiline text field, encode the entire string.
  • %0A is the encoded value for enter, so split the encoded string wrt %0A and then slice the first and the last line.

Result:

JuhiPoddar_1-1736274315456.png

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

Thanks Juhi, 

jaycoover
Tera Expert

This gets close as long as you don't have multiple line breaks
/Hello.World.please.pick.me.from/s

If you could simply parse out the line breaks before you ran the match it would be trivial, but it seems from your example output you need to retain them.

Sorry, this -> 
/Hello.World.please.pick.me.from.the.annoying.bla/s

This is actually a little more accurate -
/Hello\sWorld\splease\spick\sme\sfrom\sthe\sannoying\sbla/

If you need to account for multiple concurrent line breaks, you could use \s+ instead of \s

Fascinating problem that doesn't exist in some other languages like C# where there is a flag to just ignore all line breaks. The \m flag in javascript seems to only work in conjuction with $ and ^