Home > Software engineering >  Displaying a part of a string
Displaying a part of a string

Time:02-28

I get logs from the api in the format shown below. How do I just display the part after the last \n and time. The digits are time. Can it be done with some array methods or would I need regex here?

1645549901022 Running\n1645549901022 Process started\n1645549901022 START - Cloning repo\n1645549934999 Removed dumps

The part that I want to display is just Removed dumps

edit: \n is getting ignored so I think I would need to match the last digits/time

CodePudding user response:

One line:

const input = '1645549901022 Running\n1645549901022 Process started\n1645549901022 START - Cloning repo\n1645549934999 Removed dumps';
const output = input.split(/[\n(\d )] /);

console.log(output[output.length-1].trim());

Result: Removed dumps

CodePudding user response:

This is an example:

<script>
   logString = "1645549901022 Running\n1645549901022 Process started\n1645549901022 START - Cloning repo\n1645549934999 Removed dumps";

   arr = logString.split(" ");
    text = arr[arr.length-1]   ' '    arr[arr.length-2];
    alert(text);
</script>
  • Related