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

为何Regex无法应用于Obsidian Dataview的最终输出?

问题解决:Obsidian Dataview文本格式化正则修正

问题原因

你的正则表达式没有过滤掉文件名的.md后缀,且全局标志在单条字符串处理中多余,导致输出不符合预期。

修正后的代码

const inputSentences = [
"- [[Note example 3.md|Note example 3]]",
"- [[Note example 2.md|Note example 2]]",
"- [[Note example 1.md|Note example 1]]"
]

function transformSentence(input) {
  if (typeof input !== 'string') {
    return input;
  }
  // 匹配并提取.md后缀前的文件名
  const regex = /-\s*\[\[([^|]+)\.md\|[^]]+\]\]/;
  return input.replace(regex, '- $1');
}

inputSentences.forEach(inputSentence => {
  const outputSentence = transformSentence(inputSentence);
  console.log(outputSentence);
})

关键调整说明

  • 正则表达式新增.md匹配规则,精准捕获不带后缀的文件名部分,避免输出包含.md
  • 移除全局标志g,因为每条输入仅存在一个匹配项,全局标志无实际作用
  • 替换后输出即为- Note example X的预期格式

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 07:18:20