title: How to concatenate strings for building a regex?
body: <p>I'm using JavaScript and i have a question about regex. I haven't found exactly what i'm asking so here it goes.</p>
  
  <p>I have a super long regex and i would like to split it into a bunch of more smaller strings to make clear what the regex is validating.</p>
  
  <p>I know that it is possible to split the long regex into smaller ones by doing something like (and this way actually works)</p>
  
  <pre><code>const patternOne = /(\w+:{0,1}\w*@)?/;
  const patternTwo = /([a-zA-Z0-9.-]+)/;
  
  const pattern = new RegExp(`^${patternOne.source}${patternTwo.source}$`, 'i');
  </code></pre>
  
  <p>But i would like to omit the <code>.source</code> and do something more simpler like</p>
  
  <pre><code>const patternOne = '(\w+:{0,1}\w*@)?';
  const patternTwo = '([a-zA-Z0-9.-]+)';
  
  const pattern = new RegExp(`/^${patternOne}${patternTwo}$/`, 'i');
  </code></pre>
  
  <p>Just using the strings</p>
  
  <p>But when i do for example <code>pattern.test(myString)</code> i get an error that the regex is invalid.</p>
  
  <p>Maybe i forgot to escape one of my characters? Or is not possible to use just the strings?</p>