title: truncating a string after and before a word in javascript
body: <p>How do I truncate a string after or before a pattern? </p>
  
  <p>Say if I have a string <code>"abcdef"</code> I need to truncate everything after <code>"abc"</code> so the output will be: </p>
  
  <p><code>def</code></p>
  
  <p>and if i say truncate before <code>"def"</code> the output should be:</p>
  
  <p><code>abc</code></p>
  
  <p>Below is the code that I tried</p>
  
  <pre class="lang-js prettyprint-override"><code>var str1 = "abcdefgh";
  var str2 = str1.substr(str1.indexOf("abc"), str1.length);
  console.log(str2);
  </code></pre>
  
  <p>I didn't get the output.</p>
  
  <p>I'm stuck here any help will be much appreciated.</p>