gpt4 book ai didi

javascript - 正则表达式 .exec() 丢失匹配字符串的第一个数字

转载 作者:行者123 更新时间:2023-12-03 06:23:02 24 4
gpt4 key购买 nike

我正在构建一个 NodeJS JavaScript 应用程序,并尝试使用正则表达式来处理一些 Markdown 文件。

我有以下代码:

var title = /```.(.*?\n)/.exec(codeBlock);
console.log('Code: ' + codeBlock);
console.log('Title: ' + title[1]);

输出是:

Code: ```TypeScript
animationSet = new animationModule.Animation([{
target: view,
rotate: 360,
duration: 3000,
iterations: Number.POSITIVE_INFINITY,
curve: view.ios ? UIViewAnimationCurve.UIViewAnimationCurveLinear : new android.view.animation.LinearInterpolator
}]);
animationSet.play().catch((e) => {
console.log("Animation stopped!");
});
// Call animationSet.cancel() to stop it;
```
Title: ypeScript

注意,我正在寻找第一个字符,而不是完整的“标题”。如果我添加一个空格,使得代码块的第一行是 ``` TypeScript 然后我会得到 Title: TypeScript 的输出。

我的第一个 Angular 色去哪里了,我怎样才能把它找回来!

最佳答案

Helpful tool

嗯,您的捕获组在第一个字符之后开始

/```.(.*?\n)/ 

它说:

  • ``` - 这 3 个字符
  • 。 - 任意 Angular 色之一<- 这是您消耗但未捕获的 Angular 色
  • ( - 启动捕获组
  • ...等等

所以有效的模式是:

```(.*?\n)

关于javascript - 正则表达式 .exec() 丢失匹配字符串的第一个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38778403/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com