- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 09:02 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 10:27 AM
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:
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 09:17 AM
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]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 09:29 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 09:33 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 09:38 AM
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