VSCODE片段 - 基于用户输入长度动态生成字符
我有这个用于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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试此片段:
{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:
{1/./-/g}
will replace each character in$1
with a-
. In that way you are effectively counting the number of characters in$1
.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.