将标签转换为小写的正则表达式 – 调整 VS Code 代码片段

huangapple go评论59阅读模式
英文:

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}}}",

huangapple
  • 本文由 发表于 2023年3月31日 04:02:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892529.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定