简体   繁体   English

如何使用正则表达式在 Visual Studio Code 的代码片段中“大写和替换”?

[英]How to use Regex to 'capitalize and replace' in Visual Studio Code's snippets?

I want to create a snippet on Visual Studio Code.我想在 Visual Studio Code 上创建一个片段。

I tried to manually join the Regex but it never worked like my expect:我试图手动加入正则表达式,但它从来没有像我预期的那样工作:

input: idss-static-frame.spec输入: idss-static-frame.spec

expected result: IdssStaticFrame预期结果: IdssStaticFrame

my Regex: ${TM_FILENAME_BASE/((\\w+(?=\\-))*(\\w+(?=\\.))*)((\\-)*)/${1:/capitalize}/g}我的正则表达式: ${TM_FILENAME_BASE/((\\w+(?=\\-))*(\\w+(?=\\.))*)((\\-)*)/${1:/capitalize}/g}

actual result: IdssStaticFrame.spec实际结果: IdssStaticFrame.spec

I couldn't remove .spec string我无法删除.spec字符串

For this kind of filenames, you may use对于这种文件名,您可以使用

"${TM_FILENAME_BASE/(\\w+)(?:-|\\.\\w+$)/${1:/capitalize}/g}",

See the regex demo查看正则表达式演示

Details细节

  • (\w+) - Group 1: one or more word chars (\w+) - 第 1 组:一个或多个单词字符
  • (?:-|\.\w+$) - - or . (?:-|\.\w+$) - -. + 1 or more word chars at the end of string. + 字符串末尾的 1 个或多个单词字符。

You may use您可以使用

"${TM_FILENAME_BASE/([^-]+)(?:-|\\.\\w+$)/${1:/capitalize}/g}",

To match file names that contain chars other than just word chars as [^-]+ matches 1 or more chars other than - .匹配包含除单词字符以外的字符的文件名,因为[^-]+匹配除-以外的 1 个或多个字符。

"${TM_FILENAME_BASE/(\\w+)[-\\.]|.+/${1:/capitalize}/g}",

see regex101 .regex101

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio 代码中的 Bootstrap 3 代码段 - Bootstrap 3 Snippets in visual studio code Visual Studio代码 - 如何将用户代码段放在列表顶部 - Visual Studio Code - How to put User Snippets at the top of the list 如何在 Visual Studio 代码 javascript 片段中创建自定义函数片段? - How to create a custom function snippet in visual studio code javascript snippets? Visual Studio代码。 正则表达式仅搜索/替换撇号 - Visual Studio Code. Regex to search/replace just the apostrophe 如何使用正则表达式将街道地址大写? - How to use Regex to capitalize a street address? 如何正确结合正则表达式和替换。 大写单词的方法 - How to correctly combine Regex and the replace. method to Capitalize a word Visual Studio 2008中的Javascript片段 - Javascript Snippets in Visual Studio 2008 MDN 上的澄清“使用函数而不是评估代码片段”——为什么? - Clarifications on MDN "Use functions instead of evaluating snippets of code" - why's that? 如何使用jQuery的.each()替换文本/正则表达式? - How do I use jQuery's .each() to replace text / regex? 如何在jquery中使用replace和regex在单词中大写字符之前插入空格 - How to insert space before capitalize character in a word using replace and regex in jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM