英文:
Regex - Adapting a VS Code Snippet to convert a label to downcase
问题
I am trying to create a snippet for VS Code that creates a label based on the "Section" name:
"Headings | Section": {
"prefix": "sec",
"body": [
"\\section{${1:section_name}} % (fold)",
"\t\\label{sec:${2:${1/([^a-zA-Z0-9]+)/_/g}}}",
"\t$0"
],
"description": "Create a new section with an automatically generated label"
},
All is working fine to remove non-alphanumerical characters and replace spaces with underscores, but I am trying (and currently unable) to also make the label lowercase (\downcase). Would be grateful for any assistance.
I have tried implementing variations such as:
"\t\\label{sec:${2:${1/([^a-zA-Z0-9]+)/_/g}${1/(.)/${1:/downcase}/g}}}",
For the label and not getting the desired result.
英文:
I am trying to create a snippet for VS Code that creates a label based on the "Section" name:
"Headings | Section": {
"prefix": "sec",
"body": [
"\\section{${1:section_name}} % (fold)",
"\t\\label{sec:${2:${1/([^a-zA-Z0-9]+)/_/g}}}",
"\t$0"
],
"description": "Create a new section with an automatically generated label"
},
All is working fine to remove non alphanumerical characters and replace spaces with underscores, but I am trying (and currently unable) to also make the label lowercase (\downcase). Would be grateful for any assistances.
I have tried implementing variations such as:
"\t\\label{sec:${2:${1/([^a-zA-Z0-9]+)/_/g}${1/(.)/${1:/downcase}/g}}}",
For the label and not getting the desired result.
答案1
得分: 1
"Headings | Section": {
"prefix": "sec",
"body": [
"\\section{${1:section_name}} % (fold)",
// "\t\\label{sec:${2:${1/([^a-zA-Z0-9]+)/_/g}}}",
"\t\\label{sec:${2:${1/([^\\s])|(\\s)/${1:/downcase}${2:+_}/g}}}",
"\t$0"
],
"description": "创建一个带有自动生成标签的新部分"
},
"\t\\label{sec:${2:${1/(\\w)|(\\s)(?=.*\\w)|[^\\s]|\\s/${1:/downcase}${2:+_}/g}}}""
英文:
I think this what you are looking for - downcasing the second instance of $1
:
"Headings | Section": {
"prefix": "sec",
"body": [
"\\section{${1:section_name}} % (fold)",
// "\t\\label{sec:${2:${1/([^a-zA-Z0-9]+)/_/g}}}",
"\t\\label{sec:${2:${1/([^\\s])|(\\s)/${1:/downcase}${2:+_}/g}}}",
"\t$0"
],
"description": "Create a new section with an automatically generated label"
},
${1/([^\\s])|(\\s)/${1:/downcase}${2:+_}/g}
this will get all NON-whitespace characters in capture group 1 and all whitespace characters in capture group 2.
All group 1's will be downcased.
If there is a group 2, it will be replaced by an underscore due to ${2:+_}
which is a conditional replacement you can put into snippets which says if group 2 insert an _
.
Use the following form if you want to change something like HO**WDY* T**HerE **
to howdy_there
.
"\t\\label{sec:${2:${1/(\\w)|(\\s)(?=.*\\w)|[^\\s]|\\s/${1:/downcase}${2:+_}/g}}}",
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论