将正则表达式匹配保存到的全局变量名称是什么?

时间:2013-10-09 07:23:39

标签: javascript regex

以前使用的正则表达式。

/.*/.exec('meow')

{thingIforgot} [0] =='meo'

现在我知道上面的表达式会返回结果。我特意想记住我不能谷歌的变量名称。

==============================

 while((carry = /{[^%]\/*?\}/.exec(string))){
    var _results
    if(/\{s\d+\}/.exec(carry[0])){

    } else
    if(/\{s\d+\}/.exec(carry[0])){

    } else{
      throw new Error('MARLFOREMD STRING '+string)
    }

我知道我不必分配那个表达式= /

2 个答案:

答案 0 :(得分:0)

我相信您可能正在寻找一些已弃用的Firefox功能:

  

Gecko 8.0 note(Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5)

     

在Gecko 8.0之前(Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5),exec()   实施不正确;当它被调用时没有参数,它   将匹配前一个输入的值(RegExp.input   property)而不是字符串“undefined”。这是固定的;   现在/undefined/.exec()正确导致['undefined'],而不是   错误。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#Calling_exec()_with_no_parameters_in_old_Gecko_versions

答案 1 :(得分:0)

与其他一些语言especially Perl不同,JavaScript / ECMAScript没有与RegExp匹配相关的全局变量。

有关上一场比赛的信息仅存储在返回的值/集合和属性on the RegExp itself中:

var pattern = /./g;
var found = pattern.exec('a');

console.log(found[0], found.index, pattern.lastIndex); // "a", 0, 1

但是,请注意lastIndex只会增加“全球RegExp

var pattern = /{[^%]\/*?\}/g;
//                         ^

while((carry = pattern.exec(string))){
    // ...
}

您可以看到.exec() in the spec所做的一切。