0

I am trying to extract last string from a sentence.

var output is

[info] Sikuli vision engine loaded.
[info] Windows utilities loaded.
[info] VDictProxy loaded.
The arguments are:  ['E:/automation.sikuli/automation.py', '{"command":"sel_medi
a","value":"5X7_on_letter"},{"command":"sel_printer","value":"cutepdf"},{"comman
d":"sel_tray","value":"formsource"},{"command":"print","value":"print"}']
5X7_on_letter not Selected

From this How can I get the last 5X7_on_letter not Selected only?

user2798227
  • 853
  • 1
  • 16
  • 31
Psl
  • 3,830
  • 16
  • 46
  • 84

1 Answers1

2

One possible approach:

var lastLine = output.match(/.*$/)[0];

Demo, regex101 playground.

Explanation: /.*$/ pattern matches all the symbols preceding the end of the string except newline ones. This will essentially cover only the last line of that string.

A regex-free alternative is using lastIndexOf to get the position of the last EOL, then taking the rest of the string. Like this:

var lastLine = output.slice(output.lastIndexOf('\n') + 1);    
raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • am not getting using this – Psl Apr 04 '14 at 08:52
  • Sorry, what? You don't have the expected result, or you don't understand how it works? – raina77ow Apr 04 '14 at 08:57
  • actually that is not working ,-...the output string is the response.but using ur code it is not working i dont know – Psl Apr 04 '14 at 09:01
  • Where's this string (value of `output`) taken from? What happens if you just `output.match(/not Selected/)[0]` instead? And what exactly does *'not working'* mean - do you get an error, an empty string, or the whole string in `lastLine`? – raina77ow Apr 04 '14 at 09:03
  • 1
    You shouldn't have quotes around your regex. ```var lastLine = output.match(/.*$/)[0];``` – Brett Apr 04 '14 at 09:18
  • sorry :-( ..sorry ].. i just console.log(output) it got the above string.actually that output returns an array when i use output =output.split("\n").ur answer is correct.But in my case i got the last string by output[output.length-1] – Psl Apr 04 '14 at 09:40