代码之家  ›  专栏  ›  技术社区  ›  trixo

将升华文本3片段中文件名的第一个字母更改为小写的正则表达式

  •  1
  • trixo  · 技术社区  · 7 年前

    我想为无状态组件创建以下react代码段。

    import React from 'react';
    const fileName= (props) => {
    
    
    }
    
    export default fileName;
    

    <snippet>
     <content><![CDATA[
    import React from 'react';
    const ${1:${TM_FILENAME/(.+)..+..+/$1/}} = (props) => {
    
    
    }
    
    export default ${1:${TM_FILENAME/(.+)..+..+/$1/}};
    ]]></content>
     <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
     <!-- <tabTrigger>hello</tabTrigger> -->
     <tabTrigger>less</tabTrigger>
     <!-- Optional: Set a scope to limit where the snippet will trigger -->
     <!-- <scope>source.python</scope> -->
    </snippet>
    

    哪个输出

    import React from 'react';
    const FileName= (props) => {
    
    
    }
    
    export default FileName;
    

    1 回复  |  直到 7 年前
        1
  •  1
  •   Wiktor Stribiżew    7 年前

    根据 SublimeText snippet documentation ,这里的正则表达式是 Boost 并且它支持 replacement patterns .

    ${1:${TM_FILENAME/(.+)..+..+/\l$1/}}
                                 ^^
    

    这个 \l

    其他选择:

    \l  Causes the next character to be outputted, to be output in lower case.
    \u  Causes the next character to be outputted, to be output in upper case.
    \L  Causes all subsequent characters to be output in lower case, until a \E is found.
    \U  Causes all subsequent characters to be output in upper case, until a \E is found.
    \E  Terminates a \L or \U sequence.
    

    请注意,您的模式看起来并不好,因为它捕获了任何1个或多个字符( (.+) )直到最后四个( ..+..+ 匹配一行中最后4个字符(由于第一个贪婪模式)。它可能无法满足您的需要。

    如果您计划捕获到最后一个点(如果存在)之前的任何文本,请使用

    ${1:${TM_FILENAME/^(.*?)(\.[^.]*)?$/\l$1/}}
    

    哪里

    • ^ -字符串开头
    • (.*?) -第一组( $1
    • (\.[^.]*)? -第2组(可选):a . 然后是任何0+字符,而不是 .
    • $ -结束字符串。