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

jcmings
Mega Sage

Just as a note, this is the kind of question that ChatGPT is excellent for. It could tell you exactly how to do the regex:

 

// Original string
const originalString = "**** Begin bla bla bla-  ****\n\nHello World please pick me\n\nfrom the annoying bla\n\n**** End bla bla bla ****";

// Regular expression pattern to match the text between the markers
const regexPattern = /Begin bla bla bla-  \*\*\*\*\n\n(.*?)\n\n\*\*\*\* End bla bla bla \*\*\*\*/s;

// Extract the matched text
const matchedText = originalString.match(regexPattern);

//Output would be stored in matchedText[1]

 

ashu1612
Tera Contributor

Thanks for your reply I had pasted the string just as example the input string may have more than 2 lines , I need some generic regex to parse all the lines within specified text

 

The regex is looking for data between **** Begin bla bla bla-  **** and **** End bla bla bla ****. Anything between those two lines will be stored in matchedText[1]. So long as your string and ends with those phrases, you'll be fine. 

 

If you have different starting & ending phrases (maybe you use a series of characters, like &&&, for example), you'll need to adjust regexPattern. ChatGPT can help you with this.

jaycoover
Tera Expert

You'll want your regex to execute in multiline mode. A great little tutorial right here -
https://www.stefanjudis.com/today-i-learned/multiline-mode-in-javascript-regular-expressions/

 

-Jay