[英]Extract last string from a sentence using node.js
I am trying to extract last string from a sentence. 我试图从句子中提取最后一个字符串。
var output
is 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
From this How can I get the last 5X7_on_letter not Selected
only? 从这个如何才能获得最后的5X7_on_letter not Selected
?
One possible approach: 一种可能的方法:
var lastLine = output.match(/.*$/)[0];
Demo , regex101 playground . 演示 , regex101游乐场 。
Explanation: /.*$/
pattern matches all the symbols preceding the end of the string except newline
ones. 说明: /.*$/
pattern匹配除newline
之外的字符串newline
之前的所有符号。 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. 一个无正则表达式的替代方法是使用lastIndexOf
来获取最后一个EOL的位置,然后使用其余的字符串。 Like this: 像这样:
var lastLine = output.slice(output.lastIndexOf('\n') + 1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.