Home > Back-end >  Return value from 2nd line in string
Return value from 2nd line in string

Time:09-20

I am trying to return the 2nd line from a field containing a string. I have tried indexof and split, and it gets me close, but have not had success using the newline \n.

How can I return "Assignment Group: TI-BSM-Tech Monitoring" or ideally "TI-BSM-Tech Monitoring" from the string below.

Call-out:  true 
Assignment Group:  TI-BSM-Tech Monitoring 
Data String - index=cisco_ise sourcetype=cisco:ise:syslog source=* Alert when critical services are no longer logging Hourly

Some code I have tried

var description = "Call-out:  true Assignment Group:  TI-BSM-Tech Monitoring next String - index=cisco_ise sourcetype=cisco:ise:syslog source=* Alert when critical services are no longer logging Hourly".split('Assignment Group:')[1];

var assignment = description.substring(description.indexOf('Group:  ')   2);

console.log(assignment);

Thanks

CodePudding user response:

This is not a pretty answer, but not sure how else you'd do it. Split by Assignment Group:, then "\n". You have to use double-quotes to trap a newline character

let description = document.querySelector('textarea').value;

let assignment = description.split('Assignment Group:')[1].split("\n")[0].trim();

console.log(assignment);
<textarea rows="5" cols="50">
Call-out:  true 
Assignment Group:  TI-BSM-Tech Monitoring 
Data String - index=cisco_ise sourcetype=cisco:ise:syslog source=* Alert when critical services are no longer logging Hourly
</textarea>

CodePudding user response:

This can also be done with a regular expression

let description = document.querySelector('textarea').value;

// skip everything until Assignment Group, read everything that isn't a newline, discard everything else
// m is the multi-line flag

let assignment = description.replace(/^.*\nAssignment Group:\s ([^\n] ).*\n.*$/m,'$1');

document.getElementById("output").textContent = assignment;
<textarea rows="5" cols="50">
Call-out:  true 
Assignment Group:  TI-BSM-Tech Monitoring 
Data String - index=cisco_ise sourcetype=cisco:ise:syslog source=* Alert when critical services are no longer logging Hourly
</textarea>
<br>
<output id="output">
</output>

  • Related