Regex to count the instances of '?' that are in quotes
<p>I'm trying to make a regex that will count every question mark that is inside of quotes. This regex is being tested in javascript but I intend to use it for PHP if that matters. I have something that kind of works but not well enough.</p>

<p>Here it is.</p>

<pre><code>/(\"|\')(([^\"\'\\]|\\.)*)\?(([^\"\'\\]|\\.)*(\"|\'))/g
</code></pre>

<p>As you can probably see I also want to ignore escaped quotes.</p>

<p>Say I have the string <code>"hello? \"world?\""</code>. This will return 1 which is correct.</p>

<p>But as for this <code>"hello? \"world??\""</code>. This will also return 1, but what I want is 2. How can I accomplish this?</p>

<p>Also extra love if I can get a regex that is the exact opposite of this (counting question marks that are NOT in quotes).</p>

<p>Here's the whole function used for this test if it helps.</p>

<pre><code>function countTest(str) {
    regx = /(\"|\')(([^\"\'\\]|\\.)*)\?(([^\"\'\\]|\\.)*(\"|\'))/g;

    test = str.match(regx);
    test = test ? test.length : 0;

    console.log(test);
}
</code></pre>

<p>EDIT:</p>

<p>Also! I noticed from my own typo in this question the string <code>hello \"world?\'"</code> will also return 1. That seems easy to fix though.</p>
