title: PCRE implementing negative lookahead AND behind working together?
body: <h1>Hi all,</h1>
  
  <p>I'm having a bit of a problem that I am obviously unable to solve. It involves PCRE implementing negative lookahead AND behind conditions that should (but obviuosly don't) work together.</p>
  
  <h1>What I'm trying to accomplish:</h1>
  
  <p>I have a string containing a constant in a JavaScript compliant syntax. This string's syntax needs to be converted to be adhering to JSON standards.</p>
  
  <h1>The problem I encounter:</h1>
  
  <p>When I try to encapsulate the object property keys with quotation marks, I need to distinct "real" property keys from strings contained in an array, that happen to look like property keys.</p>
  
  <h1>Example input (all JS):</h1>
  
  <pre><code>const Const = {
    propertyKeyA: "someValue",
    propertyKeyB: ["ThisIsMyHeadache:ItShouldNotBeChanged"]
  };
  </code></pre>
  
  <h1>Desired output:</h1>
  
  <pre><code>{
    "propertyKeyA": "someValue",
    "propertyKeyB": ["ThisIsMyHeadache:ItShouldNotBeChanged"]
  }
  </code></pre>
  
  <h1>My PCRE approach:</h1>
  
  <pre><code>$output = preg_replace(
    '~(?:^|\b)    (?![\'"])  (\w+)  (?&lt;![\'"])     :~mx',
    '"\1":',
    $input
  );
  </code></pre>
  
  <p>which leads to:</p>
  
  <pre><code>{
    "propertyKeyA": "someValue",
    "propertyKeyB": [""ThisIsMyHeadache":ItShouldNotBeChanged"]
  }
  </code></pre>
  
  <p>Notice the double quotation marks in the array definition. It seems to me as if the conditions do not work at all.</p>
  
  <p>Does anyone have an idea on how to solve this? It would be <em>immensely</em> appreciated!</p>
  
  <p>Best,
  Chris</p>