title: Finding, storing and replacing text in Visual Studio Code
body: <p>I have a lot of text in my editor with references in brackets. So for example. </p>
  
  <blockquote>
    <p>"This is text, welcome to my text [11]. We have several sources [23]. What can I do for you [33]"</p>
  </blockquote>
  
  <p>I want to find each number in between the brackets and replace that number with an anchor tag containing it, like so <code>&lt;xref ref-type="bibr" rid="R11"&gt;[11]&lt;/xref&gt;</code> and then <code>&lt;xref ref-type="bibr" rid="R23"&gt;[23]&lt;/xref&gt;</code> What I am doing now is finding the numbers in brackets with a regex ( <strong>\[.*?]</strong> ) in the editor and then copy and pasting what I want to replace. This is time consuming when there are over 100 references to replace. </p>
  
  <p>Is there a way to find, store it in a variable and then replace with said variable? </p>
  
  <p>One possible solution I came up with is using JavaScript, however I cannot get it to work quite right, as evident by my output.</p>
  
  <p>Code:</p>
  
  <pre><code>const document = 'This is text, welcome to my text [11]. We have several sources [23]. What can I do for you [33]'
  const regex = /\[.*?\]/gm
  let result = document.match(regex);
  
  let array = result
  var arrayLength = array.length;
  console.log(arrayLength);
  for (var i = 0; i &lt; arrayLength; i++) {
      console.log(document.replace(regex, '&lt;xref ref-type="bibr" rid="R' +  array[i] + '"&gt;' + array[i] + "&lt;/xref&gt;"));
  }
  </code></pre>
  
  <p>Console Output:</p>
  
  <pre><code>This is text, welcome to my text &lt;xref ref-type="bibr" rid="R[11]"&gt;[11]&lt;/xref&gt;. We have several sources &lt;xref ref-type="bibr" rid="R[11]"&gt;[11]&lt;/xref&gt;. What can I do for you &lt;xref ref-type="bibr" rid="R[11]"&gt;[11]&lt;/xref&gt;
  This is text, welcome to my text &lt;xref ref-type="bibr" rid="R[23]"&gt;[23]&lt;/xref&gt;. We have several sources &lt;xref ref-type="bibr" rid="R[23]"&gt;[23]&lt;/xref&gt;. What can I do for you &lt;xref ref-type="bibr" rid="R[23]"&gt;[23]&lt;/xref&gt;
  This is text, welcome to my text &lt;xref ref-type="bibr" rid="R[33]"&gt;[33]&lt;/xref&gt;. We have several sources &lt;xref ref-type="bibr" rid="R[33]"&gt;[33]&lt;/xref&gt;. What can I do for you &lt;xref ref-type="bibr" rid="R[33]"&gt;[33]&lt;/xref&gt;
  </code></pre>
  
  <p>As you can see, I also need to remove the brackets from the <strong>rid=</strong> attribute. I also would like to get this done in the editor itself.  If it is not possible, then I figured I can use node.js and write to the file. Any other solution would also help. Should this be done in Python? </p>