You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在JavaScript中提取指定子字符串的最简方法是什么?

JavaScript提取特定子字符串的最简方法

嘿,这有个超简洁的实现方式,用正则表达式就能精准提取你要的目标子串:

// 你的原始日志文本
const logContent = "at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) at DOCSIDE.Web.Util.ExecuteMethod(Object o, String method, Object[] arguments, String buildNumber) in C:\\Software\\DOCSIDEFINAL\\DOCSIDE\\Main\\DOCSIDEBusinessLogic\\Web\\Util.cs:line 247 +++ DOCSIDE Sproc - updateTrkRecord Line - 2203 [@@Error=50000] Type is required after FD analyst has been assigned. User is not authorized.";

// 匹配目标子串的正则表达式(转义句点避免被当作通配符)
const targetPattern = /Type is required after FD analyst has been assigned\. User is not authorized\./;

// 执行匹配并获取结果,匹配不到时返回提示
const extractedString = logContent.match(targetPattern)?.[0] || "目标子串未找到";

console.log(extractedString);

为什么这是最简方案?

  • 正则直接精准匹配你指定的完整子串,逻辑一目了然
  • 用可选链?.处理匹配失败的情况,不会报错
  • 代码量极少,一步到位就能拿到结果

内容的提问来源于stack exchange,提问作者user2748196

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:06:12