title: Regexp to match Javascript string literals with a specific keyword using Java
body: <p>I'm trying to match chunks of JS code and extract string literals that contain a given keyword using Java.</p>
  
  <p>After trying to come up with my own regexp to do this, I ended up modifying this generalized string-literal matching regexp (Pattern.COMMENTS used when building the patterns in Java):</p>
  
  <pre>
  (["'])
  (?:\\?+.)*?
  \1
  </pre>
  
  <p>to the following</p>
  
  <pre>
  (["'])
  (?:\\?+.)*?
  keyword
  (?:\\?+.)*?
  \1
  </pre>
  
  <p>The test cases:</p>
  
  <pre><code>var v1 = "test";
  var v2 = "testkeyword";
  var v3 = "test"; var v4 = "testkeyword";
  </code></pre>
  
  <p>The regexp correctly doesn't match line 1 and correctly matches line 2.</p>
  
  <p>However, in line 3, instead of just matching "testkeyword", it matches the chunk</p>
  
  <pre><code>"test"; var v4 = "testkeyword"
  </code></pre>
  
  <p>which is wrong - the regexp matched the first double quote and did not terminate at the second double quote, going all the way till the end of line.</p>
  
  <p>Does anyone have any ideas on how to fix this?</p>
  
  <p>PS: Please keep in mind that the Regexp has to correctly handle escaped single and double quote characters inside of string literals (which the generalized matcher already did).</p>