gpt4 book ai didi

javascript - 如何调整 while 循环中的变量范围以匹配 CoffeeScript 中的所有正则表达式?

转载 作者:行者123 更新时间:2023-11-30 07:42:19 25 4
gpt4 key购买 nike

我在使用 coffeescript 中的 while 循环时遇到了一些麻烦。

原函数如下,the source is this answer :

// 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;
}

它按预期工作。我已经通过 js2coffee 转换了它 CoffeeScript 是:

# Return all pattern matches with captured groups
RegExp::execAll = (string) ->

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

编译为:

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;
};

结果是:

[
[],
[],
[],
[],
[],
[],
[],
[],
[]
]

我认为这行不通,因为范围可变,因为我能看到的唯一区别是这一行:

RegExp.prototype.execAll = function(string) {
var i, match, matchArray, matches;

与原来的相比:

RegExp.prototype.execAll = function(string) {
var match = null;
var matches = new Array();
while (match = this.exec(string)) {
var matchArray = [];

请注意 matchArray 是如何限定范围的……我找不到解决这个问题的方法,但在研究过程中我发现了这些问题 Were `do...while` loops left out of CoffeeScript...?How do I declare a variable in a specific scope in coffeescript?而且我在这里几乎没有想法......除了这个 while 循环之外的任何其他匹配所有与正则表达式的方法(顺便说一下我已经尝试过 \gm 标志并且它仍然只匹配一个匹配)?或者有没有办法在 CoffeeScript 中正确设置此范围?

整个 Coffee Script 代码是:

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) ->

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, " ") )

Javascript 有效是这样的:

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, " "));

最佳答案

实际问题并不是你想的那样。实际问题是==编译为===。修复方法如下:

# Return all pattern matches with captured groups
RegExp::execAll = (string) ->
matches = []
while match = @exec string
matches.push(group for group in match)
matches

但是您可能想知道为什么 === 不起作用。因此,我向您介绍:

为什么 === 不起作用(在这种情况下)

parseInt(i) == i 应该确定 i 是否是表示整数的字符串。您可以在 JavaScript 中对一些值进行尝试,看看它是否有效。 parseInt(i) 是一个数字,而 i 是一个字符串。在 JavaScript 中,如果您在字符串和数字上使用 ==,它将隐式强制转换并且测试将如您预期的那样工作。这种隐含的强制转换通常是出乎意料的,因此 CoffeeScript 通过没有等效的 == 运算符来禁止它发生,翻译 == (并且 ) 到 === 中,它首先比较类型。在这种情况下,数字和字符串永远不会相等,因此永远不会满足 if 的条件,因此不会将任何内容压入数组。

关于javascript - 如何调整 while 循环中的变量范围以匹配 CoffeeScript 中的所有正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14078901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com