javascript - 在提示词之前去除段落文本
问题描述
我有这样的文字:
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 for Java 引擎(在这种情况下,我使用的是 Rhino)意味着结果与您在 web.xml 中获得的结果不同。这是在以下答案后发现的,该答案在网络上完美运行,甚至无法在桌面应用程序上运行。
解决方案
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"))
推荐阅读
- c# - 如何在 C# 中快速将二维数组转换为一维数组?
- javascript - 边缘上不存在 Intl.NumberFormat.formatToParts
- typescript - VS Code - 在自定义 ViewsContainer 中更新 TreeView 中的图标
- firebase - 我需要有关 Firebase 中的屏幕跟踪的指南
- python-3.x - 如何在 Python 中使用 enumerate 为内部循环指定起始索引?
- azure-iot-edge - 具有 1 个以上输入的 IoT Edge 模块失败。在一个模块中使用“input1”和“input2”测试不成功
- apache-spark - 无法从 Spark-Sql 连接到 Hive
- android - 我无法在我的 Dart 插件中运行单独的测试文件
- docker - 为什么我必须先提取图像才能让 docker rest api 返回一些东西?
- c++ - 从 unordered_multimap 中调用另一个类的函数?