使用node.js从句子中提取最后一个字符串

时间:2014-04-04 08:32:33

标签: node.js

我正在尝试从句子中提取最后一个字符串。

var output

[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

从这里我怎样才能获得最后一个5X7_on_letter not Selected

1 个答案:

答案 0 :(得分:2)

一种可能的方法:

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

Demoregex101 playground

说明:/.*$/模式匹配除newline之外的字符串末尾之前的所有符号。这基本上只包括该字符串的最后一行。

一个无正则表达式的替代方法是使用lastIndexOf来获取最后一个EOL的位置,然后使用其余的字符串。像这样:

var lastLine = output.slice(output.lastIndexOf('\n') + 1);