0

我正在尝试这样做:

render(argv,function() {

  var fileHandle = argv.output + '/docu.html';
  var regex_ca_id = new RegExp('[A-Za-z1-9]{16}#[A-Za-z1-9]{5}',"g");

  var rd = readline.createInterface({
    input: fs.createReadStream(fileHandle),
    output: process.stdout,
    terminal: false
  });

  rd.on('line', function(line) {
    if(regex_ca_id.test(line)) {
      console.log('Debug: '+regex_ca_id.test(line)+
                  '  '+regex_ca_id.exec(line)+'   '+line);
  }

  rd.close();
  process.stdin.destroy();
});

在包含以下行的 html 文件(“fileHandle”见上文)上:

<p class="img-container"><img src="UU4GBVJyst5kqS8O#732F4-50" alt="I am a picture" title="An Image"></p>
<p>Dies ist <a href="UU4GBVJyst5kqS8O#732F4-50" title="An Image">ein Beispiel</a> für einen Referenz-Link.</p>

它产生由 console.log() 行发出的输出:

Debug: false  UU47GZJyst5kqS8O#732F4   <p class="img-container"><img src="UU4GBVJyst5kqS8O#732F4-50" alt="I am a picture" title="An Image"></p>
Debug: false  UU47GZJyst5kqS8O#732F4   <p>Dies ist <a href="UU4GBVJyst5kqS8O#732F4-50" title="An Image">ein Beispiel</a> für einen Referenz-Link.</p>

输出是我没想到的。regex_ca_id.test(line) 的计算结果为真,因此 if 构造的主体开始。现在,console.log 中的相同语句评估为 false,在此之后,先前使用的 RegExp 对象上的 exec() 返回成功匹配的字符串。

附加用途:

var result = regex_ca_id.exec(line);

在 rd.on 块内将被分配为 null。

这对我来说看起来不一致,感谢您帮助我理解这种行为。

斯蒂芬

4

0 回答 0