3

I have a list of data that I need to put a ' symbol at the start of the line and at the end of the line. So the original data look like

11223334444xxx55555
11xxx223334444555xxx55
11xxxx22333444xxx455555
11xxxxx22333444455555
11xxxxxx223334444555xxx55

and I want all the line to look like

11223334444yyyy55555
11yyyy223334444555yyyy55
11yyyyx22333444yyyy455555
11yyyyxx22333444455555
11yyyyyyyy223334444555yyyy55

that is 'yyyy' replace 'xxx', how I write my code? Both typescript and javascript are perfect.


Sorry, my bad. I want develop an extension to do it, and above just an example. Many answers below just miss select full text part.

const textEditor = vscode.window.activeTextEditor;
if (!textEditor) {
    return;  // No open text editor
}

var firstLine = textEditor.document.lineAt(0);
var lastLine = textEditor.document.lineAt(textEditor.document.lineCount - 1);
var textRange = new vscode.Range(0,
    firstLine.range.start.character,
    textEditor.document.lineCount - 1,
    lastLine.range.end.character);

    textEditor.edit(function (editBuilder) {
        editBuilder.replace(textRange, '$1');
    });
});

replace function above just has one replace argument, not two, how can I replace it?

willwell
  • 51
  • 1
  • 5

4 Answers4

0

Try this:

let checkD = 11223334444xxx55555 11xxx223334444555xxx55 11xxxx22333444xxx455555 11xxxxx22333444455555 11xxxxxx223334444555xxx55 ;

checkD.replace(/xxx/g, 'yyyy');

  • string with multiple lines must be in `` (backquotes/backticks), see: https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript – Krzysztof Korman Jul 25 '18 at 09:37
0

You can try it with regex. Read more at the MDN.

Here is an array example:

let data = [ 
  "11223334444xxx55555",
  "11xxx223334444555xxx55",
  "11xxxx22333444xxx455555",
  "11xxxxx22333444455555",
  "11xxxxxx223334444555xxx55"
];

let max = data.length;
for (let i = 0; i < max; i++) {
  let regex = new RegExp("xxx", "g")
  data[i] = data[i].replace(regex, "yyyy")
}

console.log(data);

Here is a single string example:

let data = `11223334444xxx55555
11xxx223334444555xxx55
11xxxx22333444xxx455555
11xxxxx22333444455555
11xxxxxx223334444555xxx55`;

let regex = new RegExp("xxx", "g")
data = data.replace(regex, "yyyy")

console.log(data);
TessavWalstijn
  • 1,698
  • 1
  • 19
  • 36
0

I know it's late, but I was struggling with this sort of an issue and got myself to resolve it, using JS. If I understood your question right. You want to replace three 'x' letters with four 'y' letters.

I.e., turn this xxxxxx1xxx2xx into this yyyyyyyy1yyyy2xx

const textEditor = vscode.window.activeTextEditor;
if (!textEditor) {
  vscode.window.showErrorMessage("Editor Does Not Exist");
  return;
}
var m;
let fullText = editor.document.getText();
const regex = /xxx/gm; // 'g' flag is for global search & 'm' flag is for multiline.

//searching for previously declared xxx in regex and replacing it with 'yyyy'.
let textReplace = fullText.replace(regex, `yyyy`);

//Creating a new range with startLine, startCharacter & endLine, endCharacter.
let invalidRange = new vscode.Range(0, 0, editor.document.lineCount, 0);

// To ensure that above range is completely contained in this document.
let validFullRange = editor.document.validateRange(invalidRange);

while ((m = regex.exec(fullText)) !== null) {
  // This is necessary to avoid infinite loops with zero-width matches
  if (m.index === regex.lastIndex) {
    regex.lastIndex++;
  }

  editor.edit(editBuilder => {
    editBuilder.replace(validFullRange, textReplace);
  }).catch(err => console.log(err));
}

Regexp g flag is for global search to match all occurrences, without it, it'll only check for the first.

Regexp m flag is for multiline, it makes ^ and $ match at line beginnings and line endings(instead of at string), respectively.

Reference: global - JavaScript | MDN , multiline - JavaScript | MDN

Also, consider looking at the VSCode API Doc for Range & Validate Range

Shady
  • 96
  • 1
  • 9
-1

that is 'yyyy' replace 'xxx', how I write my code

split and join is what I use e.g.

str.split('yyyy').join('xxx');
basarat
  • 261,912
  • 58
  • 460
  • 511