javascript - Javascript:替换直到找到单词
问题描述
我有这个字符串,其中包括:
const string = `
/**
* tests
*/
describe("tests", () => {
it("create a role", async () => {});
});
`;
我想删除这个字符串的开头,直到it(
找到这个词。所以我最终可以有这样的东西:
it("create a role", async () => {});
});
`
我尝试使用这个正则表达式string.replace(/^(.+?)(?=-it\(|$)/gi, "");
,但仍然没有任何效果
解决方案
你可以找到位置然后剪断字符串:
const str = `
/**
* tests
*/
describe("tests", () => {
it("create a role", async () => {});
});
`;
const res = str.substring(str.indexOf('it('));
console.log(res);
推荐阅读
- flutter - 按下按钮时颤振倒计时开始
- python - 带有 flow_from_dataframe 的 conv2D 输入形状错误
- google-cloud-platform - 如何在实例化 Dataproc 工作流模板时传递和访问 Dataproc Sparkjob 参数
- ios - 多个帐户关联到一个(管理员)Firebase
- apache-tika - 带有 .Net Core 3.1 / .Net 5 的 TikaOnDotNet 失败,找不到方法:'Void System.IO.FileStream..ctor
- swift - 如何以编程方式在 Apple Watch 上撰写电子邮件?
- database - FAME:我如何组合 2 个数据对象(每个数据对象是一个系列列表)?
- python - 读取部分编码的文件(每行单独编码)
- spring - java 15中的隐藏类是什么?
- powerbi - Power BI - JSON 文件格式不起作用