VSCODE片段 - 基于用户输入长度动态生成字符

发布于 2025-01-28 13:39:15 字数 490 浏览 3 评论 0原文

我有这个用于VUE-HTML的基本片段

{
    "BANNER1": {
        "prefix": "banner",
        "body": ["<!-- ----------------", "/// $1", "--------------------- -->"]
    }
}

渲染

<!-- ----------------
/// ADD VALUES 
--------------------- -->

,它立即

,我硬编码16 Dashes -我可以根据我的$ 1动态生成的数量吗?

假设我输入“ Hello”,其中包含

我希望得到的5个字符

<!-- -----
/// Hello
----- -->

I have this basic snippet for vue-html

{
    "BANNER1": {
        "prefix": "banner",
        "body": ["<!-- ----------------", "/// $1", "--------------------- -->"]
    }
}

It render this

<!-- ----------------
/// ADD VALUES 
--------------------- -->

Right now, I hardcoded 16 dashes -

Can I dynamically generate the amount of - based on my $1?

Let's say, I enter "Hello" which contains 5 characters

I'm hoping to get this

<!-- -----
/// Hello
----- -->

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞梦里开花 2025-02-04 13:39:15

尝试此片段:

"BANNER1": {
  "prefix": "banner",
  "body": [
    "<!-- ${1/./-/g}",
      "/// $1", 
      "${1/./-/g} -->"
  ]
}

{1 /./-/ G}将用$ 1中的每个字符替换为-。这样,您正在有效地计算$ 1中的字符数。


如果要添加比$ 1的长度添加一个:

”&lt;! - $ {1/^。|(( 。)/$ {1:+ - }/g}“

第一个字符,未在Regex组中捕获。因此,如果您想使用length-2,则将第一部分更改为^.. so $ {1/^.. |(。)/$ {1:+ - } /g}或一般

$ {1/^。{n} |(。)/$ {1:+ - }/g}

并替换该n您想从$ 1中减去任何数字。

Tabstop $ 1的其余部分与一一匹配(由于全球旗帜)。然后条件

$ {1:+ - }说如果有捕获组1,则添加-。捕获1组本身永远不会添加到替换中。

Try this snippet:

"BANNER1": {
  "prefix": "banner",
  "body": [
    "<!-- ${1/./-/g}",
      "/// $1", 
      "${1/./-/g} -->"
  ]
}

{1/./-/g} will replace each character in $1 with a -. In that way you are effectively counting the number of characters in $1.

snippet length of input


What if you wanted to add one less than the length of $1, then use this:

"<!-- ${1/^.|(.)/${1:+-}/g}"

The first character, the . is not captured in a regex group. So if you wanted to use the length-2 you would change the first part to ^.. so ${1/^..|(.)/${1:+-}/g} or generically

${1/^.{n}|(.)/${1:+-}/g}

and replace that n with whatever number you want to subtract from the $1.

The rest of tabstop $1 is matched one by one (thanks to the global flag). And then the conditional

${1:+-} says if there was a capture group 1, then add a -. Capture group 1 itself is never added to the replacement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文