1

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.

starball
  • 20,030
  • 7
  • 43
  • 238
Terry Panda
  • 113
  • 4

1 Answers1

1

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}}}",
Mark
  • 143,421
  • 24
  • 428
  • 436
  • This works well to make everything lower case, the problem however is that it no longer removes non alphanumeric characters, e.g. "!" wont be removed – Terry Panda Mar 31 '23 at 21:07
  • replacing s with W above seems to work though? – Terry Panda Mar 31 '23 at 21:09
  • replacing s with W works insofar as replacing non alphanumerical characters with an _, but we actually want to delete these non alphanumerical characters – Terry Panda Mar 31 '23 at 22:07
  • Some examples would be helpful. You want to keep only `[a-zA-Z0-9]`, downcase those, replace spaces with `_` (are 2+ consecutive spaces an issue?) and remove anything else? – Mark Mar 31 '23 at 22:43
  • If you want to do what I said in the last comment, try this regex `"\t\\label{sec:${2:${1/(\\w)|(\\s)|\\W/${1:/downcase}${2:+_}/g}}}",` The only case where that is problematical is if you have a space-separated block of ONLY non- ` \w` characters like ` @@```. Is that ever the case? Like `*Howd*y* TH**erE** ***`. – Mark Mar 31 '23 at 23:10
  • I added an option in the answer to change `HO**WDY* T**HerE **` to `howdy_there`. – Mark Apr 01 '23 at 02:05
  • Awesome - thanks this is working well. How could we get the default value for the section label to be defined (e.g. default_label) rather than taking this from the section name ($1)? – Terry Panda May 02 '23 at 23:22
  • Have created a separate thread for this: https://stackoverflow.com/questions/76159504/vs-code-regex-in-snippets – Terry Panda May 02 '23 at 23:28