title: How to use RegEx with a variable for replacing
body: <p>My title might not be worded correctly sorry in advance. 
  I've looked everywhere and I honestly can't seem to figure it out. 
  I want to use a variable in a RegEx but the solution that i've found and tried to work off of works but it is not flexible for what I need. I can't seem to figure out how to convert it to a RegEx constructor object which I think is my best bet. Any help will be greatly appreciated.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>let str = 'haeandviaecy'
  
  let newString = str.replace(/(\w{3})/g, '$1 ').replace(/(^\s+|\s+$)/, '')
  //hae and via ecy</code></pre>
  </div>
  </div>
  </p>
  
  <p>replace(/(^\s+|\s+$)/,'') removes any preceding or trailing space from the string - just giving you a heads up</p>
  
  <p>So what this snippet does is go to every third and put a blank space in it. What I want is to make my code more flexible and be able to put a variable to let me choose the number for when it puts a blank space using a variable.</p>
  
  <p>right now it prints out - "hae and via ecy"
  for ex. every 4th would be - haea ndvi aecy</p>
  
  <p>I've read you cant put in variables unless it is a RegEx contructor object. When I've tried to convert it doesn't seem to work.</p>