删除提示词前的段落文本

Strip paragraph text before cue word

我有这样一条短信:

Last login: today


cat file
testMachine:root:/root# cat file
File contents
testMachine:root:/root#

我需要像这样检索信息:

testMachine:root:/root# cat file
File contents

删除最后一行很容易,但是我需要在开始时删除的行数是任意的,我需要删除所有内容,直到第一个提示词,即已知的机器名称已存储。

我试过 substring() 但它逐行删除而不是将整个文本视为一个文本,并且也删除了主机名,主机名应该保留在那里。我也试过replace(),但是我对正则表达式不熟悉,所以结果是内存异常。

编辑 1:似乎很重要的一点是,使用 JS 作为 Java 引擎(在这种情况下,我使用的是 Rhino)意味着结果与您在 web.js 中获得的结果不同。这是在下面的答案后发现的,它在网络上完美运行,甚至 运行 在桌面应用程序上都没有。

const text = ` 
Last login: today


cat file
testMachine:root:/root# cat file
File contents
testMachine:root:/root#`;
const cueWord = "testMachine:root"
const idx = text.indexOf(cueWord);
let restOftheString = text.substring(idx).split("\n");
restOftheString.pop()
console.log(restOftheString.join("\n"))