How to use regex to match all element in document.doctype.internalSubset <p>By using document.doctype.internalSubset, I have the following string, say <strong>str</strong>: </p> <pre><code>&lt;!ENTITY owl "http://www.w3.org/2002/07/owl#" &gt; &lt;!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" &gt; &lt;!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" &gt; &lt;!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" &gt; </code></pre> <p>Then, I use <strong>regex</strong> to extract the result: </p> <pre><code>result = regex.exec(str); </code></pre> <p>My <strong>expected output</strong> is an array in which: </p> <pre><code>result[0] = owl "http://www.w3.org/2002/07/owl#" result[1] = xsd "http://www.w3.org/2001/XMLSchema#" ... result[3] = rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" </code></pre> <p>So, I create this regex: </p> <pre><code>var regex = /(?:&lt;!ENTITY(.*?)&gt;)*/g; </code></pre> <p>And here is the result, of course, it's not I want: </p> <pre><code>owl "http://www.w3.org/2002/07/owl#" </code></pre> <p>Can anyone help me to figure out the errors, and how to fix all of them ?</p> <p>Note that I can use <strong>s.indexOf()</strong> to get the position of <strong><code>&lt;!ENTITY</code></strong> and <strong>></strong>, and then, use <strong>s.subString()</strong> to get the same result, but I'm learning regex now, so I want to use regex.</p> <p><strong>--------------Update---------------</strong></p> <p>Thanks to Supr, I can finally figure out the error, it seems to me that, in this case, "*" doesn't mean "match one or many time", so instead of using <code>/(?:&lt;!ENTITY(.*?)&gt;)*/g</code>, we'll use this one: <code>/(?:&lt;!ENTITY(.*?)&gt;)/g</code> (supress the *) and then loop over the string until we get all result. Here is the source:</p> <pre><code>var str = '&lt;!ENTITY owl "http://www.w3.org/2002/07/owl#" &gt;' + '&lt;!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" &gt;' + '&lt;!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" &gt;' + '&lt;!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" &gt;' var regex = /(?:&lt;!ENTITY(.*?)&gt;)/g; var results = []; while ((result = regex.exec(str)) != null) { results.push(result[1]); } console.log(str); console.log("-------------------------------"); for (var i = 0; i &lt; results.length; i++) { document.write(results[i] + "&lt;br&gt;"); } </code></pre> <p>For testing: <a href="http://jsfiddle.net/nxhoaf/jZpHv/" rel="nofollow">http://jsfiddle.net/nxhoaf/jZpHv/</a></p> <p>By the way, here is my solution using s.indexOf() and recursion:</p> <pre><code>var str = '&lt;!ENTITY owl "http://www.w3.org/2002/07/owl#" &gt;' + '&lt;!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" &gt;' + '&lt;!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" &gt;' + '&lt;!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" &gt;' var getNamespace = function(input) { var result = []; // Store the final result var temp = []; // Store the temporary result // Non trivial case if ((input != null) &amp;&amp; (input.length != 0)) { // Get the begin and and index to extract the entity's content var begin = input.indexOf("&lt;!ENTITY"); var end = input.indexOf("&gt;"); if ((begin == -1) || (end == -1) || (begin &gt;= end)) { // not found return null; } // Fix the begin index begin = begin + "&lt;!ENTITY".length; // Get the content and save it to result variable var item = input.substring(begin, end); // ok, get one item // As end &gt; begin, item is always != null item = item.trim(); // Normalize result.push(item); // Continue searching with the rest using // recursive searching temp = getNamespace(input.substring(end + 1)); // Ok, collect all of data and then return if (temp != null) { for (var i = 0; i &lt; temp.length; i++) { result.push(temp[i]); } } return result; } else { // Trivial case return null; } } // Parse it to get the result result = getNamespace(str); console.log("*************"); for (var i = 0; i &lt; result.length; i++) { document.write(result[i] + "&lt;br&gt;"); } </code></pre> <p>you can test it here: <a href="http://jsfiddle.net/nxhoaf/FNFuG/" rel="nofollow">http://jsfiddle.net/nxhoaf/FNFuG/</a> </p>