Snippet regex: match arbitrary number of groups and transform to CamelCase

后端 未结 2 548
[愿得一人]
[愿得一人] 2020-12-06 14:16

In a Visual Studio Code Snippet I\'m writing I want to convert a snake case string to camel case.

From the docs I know that the syntax is

\'${\' var         


        
2条回答
  •  余生分开走
    2020-12-06 14:49

    vs code snippet transform to camelCase:

    "test": {
        "prefix": "test",
        "body": [
            "${1:${TM_FILENAME_BASE/^(.)([a-zA-Z0-9]*)([-_.])(.*)/${1:/downcase}${2}${4:/pascalcase}/}}"
        ]
    }
    

    wrapper is ${1:},

    inner is ${TM_FILENAME_BASE/^(.)([a-zA-Z0-9]*)([-_.])(.*)/${1:/downcase}${2}${4:/pascalcase}/}

    wrapper is ${}

    inner is TM_FILENAME_BASE/^(.)([a-zA-Z0-9]*)([_\\W])(.*)/${1:/downcase}${2}${4:/pascalcase}/

    string is TM_FILENAME_BASE

    -------------------------------------------------------split

    part 1 is /^(.)([a-zA-Z0-9]*)([-_.])(.*)

    part 1.1 is ^(.)$1, first character

    part 1.2 is ([a-zA-Z0-9]*)$2

    part 1.3 is ([_\\W])$3, delimiter, ignore

    part 1.4 is (.*)$4

    -------------------------------------------------------replace

    part 2 is /${1:/downcase}${2}${4:/pascalcase}/

    part 2.1 ${1:/downcase} → lowercase

    part 2.2 ${2} → normal

    part 2.3 ${4:/pascalcase}


    for-bar.service.js → fooBarService


    Not strong enough, but enough.

提交回复
热议问题