Match the beginning of a file in a VSCode regex search

后端 未结 1 1131
鱼传尺愫
鱼传尺愫 2021-01-14 15:40

I\'m trying to match the beginning of a file in a VSCode regex search to find and remove the following pattern:

//
Anything else to leave in place
etc.


        
相关标签:
1条回答
  • 2021-01-14 15:54

    The beginning of a file in Visual Studio Code regex can be matched with

    ^(?<![.\n])
    ^(?<![\w\W])
    ^(?<![\s\S\r])
    

    You may use

    Find What: ^//\n([\s\S\r]*)
    Replace With: $1

    Or, since nowadays VSCode supports lookbehinds as modern JS ECMAScript 2018+ compatible environments, you may also use

    Find What: ^(?<![\s\S\r])//\n
    Replace With: empty

    If you wonder why [\s\S\r] is used and not [\s\S], please refer to Multi-line regular expressions in Visual Studio Code.

    Details

    • ^ - start of a line
    • // - a // substring
    • \n - a line break
    • ([\s\S\r]*) - Group 1 ($1): any 0 or more chars as many as possible up to the file end.

    The ^(?<![\s\S\r])//\n regex means:

    • ^(?<![\s\S\r]) - match the start of the first line only as ^ matches start of a line and (?<![\s\S\r]) negative lookbehind fails the match if there is any 1 char immediately to the left of the current location
    • //\n - // and a line break.
    0 讨论(0)
提交回复
热议问题