Search code examples
javascripttypescriptvisual-studio-codevscode-extensionsvscode-debugger

Get selected text separated by lines


I am trying to get the selected text separated by lines to make a VSCODE Extension.

    const document = editor.document;
    const selection = editor.selection;
    const position = editor.selection.end;
    const word = document.getText(selection);

I didnt find the command to separate the selection in array for example.

Someone has the solution and the link of the API, for document constructor ?

I tried a .split("/n") without result too.


Solution

  • You can use regex to identify the newline. In Unix and Windows systems there are two types of characters that can cause newlines.

    • CR or \r - Carriage Return (Windows)
    • LF or \n - Line Feed (Unix)

    So in you system you should split the selected string by both chracters.

    const splittedStringArray = selectedText.split(/\r?\n/);