简体   繁体   中英

Extract last string from a sentence using node.js

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?

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);    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM