Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to adjust the variable scope in while loop for matching all regex in CoffeeScript?
    primarykey
    data
    text
    <p>I'm having a little trouble with while loops in coffeescript.</p> <p>The original function is as the following and <a href="https://stackoverflow.com/a/8877261/751969">the source is this answer</a>: </p> <pre><code>// Return all pattern matches with captured groups RegExp.prototype.execAll = function(string) { var match = null; var matches = new Array(); while (match = this.exec(string)) { var matchArray = []; for (i in match) { if (parseInt(i) == i) { matchArray.push(match[i]); } } matches.push(matchArray); } return matches; } </code></pre> <p>It works as intended. I have converted it via <a href="http://js2coffee.org/" rel="nofollow noreferrer">js2coffee</a> and the coffee script is:</p> <pre><code># Return all pattern matches with captured groups RegExp::execAll = (string) -&gt; matches = new Array() match = null while match = @exec(string) matchArray = new Array() for i of match matchArray.push match[i] if parseInt(i) is i matches.push matchArray matches </code></pre> <p>Which compiles to:</p> <pre><code>RegExp.prototype.execAll = function(string) { var i, match, matchArray, matches; matches = new Array(); match = null; while (match = this.exec(string)) { matchArray = new Array(); for (i in match) { if (parseInt(i) === i) { matchArray.push(match[i]); } } matches.push(matchArray); } return matches; }; </code></pre> <p>The result is:</p> <pre><code>[ [], [], [], [], [], [], [], [], [] ] </code></pre> <p>I think this doesn't work because of the variable scope since the only difference I can see is this line:</p> <pre><code>RegExp.prototype.execAll = function(string) { var i, match, matchArray, matches; </code></pre> <p>versus the original:</p> <pre><code>RegExp.prototype.execAll = function(string) { var match = null; var matches = new Array(); while (match = this.exec(string)) { var matchArray = []; </code></pre> <p>Notice how <code>matchArray</code> is scoped out... I couldn't find a way around this, but as I was researching I found these questions <a href="https://stackoverflow.com/questions/6052946/were-do-while-loops-left-out-of-coffeescript">Were `do...while` loops left out of CoffeeScript...?</a> and <a href="https://stackoverflow.com/questions/8005505/how-do-i-declare-a-variable-in-a-specific-scope-in-coffeescript">How do I declare a variable in a specific scope in coffeescript?</a> and I'm pretty much out of ideas here... Any other way of matching all with regexp other than this while loop (by the way I have tried <code>\gm</code> flags and it still hits only one match)? Or is there a way of getting this scope right in coffee script?</p> <p>The whole Coffee Script code is :</p> <pre><code>fs = require 'fs' text = fs.readFileSync('test.md','utf8') #console.log text regex = /^(?:@@)(\w+):(.*.)/gm # Return all pattern matches with captured groups RegExp::execAll = (string) -&gt; matches = new Array() match = null while match = @exec(string) matchArray = new Array() for i of match matchArray.push match[i] if parseInt(i) is i matches.push matchArray matches res = regex.execAll text #console.log regex.exec text console.log (JSON.stringify(res, null, " ") ) </code></pre> <p>And the Javascript <strong>that works</strong> is this:</p> <pre><code>var fs, regex, res, text; fs = require('fs'); text = fs.readFileSync('test.md', 'utf8'); regex = /^(?:@@)(\w+):(.*.)/gm; // Return all pattern matches with captured groups RegExp.prototype.execAll = function(string) { var match = null; var matches = new Array(); while (match = this.exec(string)) { var matchArray = []; for (i in match) { if (parseInt(i) == i) { matchArray.push(match[i]); } } matches.push(matchArray); } return matches; } res = regex.execAll(text); //console.log(regex.exec(text)); console.log(JSON.stringify(res, null, " ")); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload