尝试将 RegEx 与 promise 返回的文本匹配——得到空字符串

Trying to match RegEx to text returned by promise -- getting empty string

我正在使用 PDF.js 从 PDF 中获取文本,然后使用 RegEx 对其进行解析。 parsetext 函数接受一个 text 参数,该参数由一个承诺 return 编辑:

 gettext: function(url){
     var self = this;
     var data = url;
     console.log('attempting to get text');
     return pdfjs.getDocument(data).then(function(pdf) {
         var pages = [];
         for (var i = 0; i <= 1; i++) {
             pages.push(i);
         }
         return Promise.all(pages.map(function(pageNumber) {
             return pdf.getPage(pageNumber + 1).then(function(page) {
                 return page.getTextContent().then(function(textContent) {
                     return textContent.items.map(function(item) {
                         return item.str;
                     }).join(' ');
                 });
             });
         })).then(function(pages) {
             return pages.join("\r\n")
         });
     }).then(function(pages){
         self.parsetext(pages);      
     });        
 },

 parsetext: function(text){

     var rx = /Seite((\S+)\s+\S.*?)(?=\s*)/;
     var s = text;
     var m = s.match(rx) || ["", ""];
     console.log(m[1] + ' is the matched text');  //   returns '  is the matched text'
 }

m[1] 应该是 return 一个很长的字符串。

删除了在未使用捕获组时尝试从 String.match() 中获取捕获组的混淆案例——主要问题仍未解决,因此这不是重复的。

问题可能出在哪里? RegEx 看起来不错,所以我只能想象这是 gettext 而不是 return 在 parsetext 之前 运行 的完整字符串的结果。但这不是承诺所确保的吗?

我认为没有 return 值不是问题,因为倒数第二个 promise 是 returning 字符串。为了证明这一点,我添加了一个 console.log 来显示正在 returned:

 gettext: function(url){
     var self = this;
     var data = url;
     console.log('attempting to get text');
     return pdfjs.getDocument(data).then(function(pdf) {
         var pages = [];
         for (var i = 0; i <= 1; i++) {
             pages.push(i);
         }
         return Promise.all(pages.map(function(pageNumber) {
             return pdf.getPage(pageNumber + 1).then(function(page) {
                 return page.getTextContent().then(function(textContent) {
                     return textContent.items.map(function(item) {
                         return item.str;
                     }).join(' ');
                 });
             });
         })).then(function(pages) {
             return pages.join("\r\n")
         });
     }).then(function(pages){
         self.parsetext(pages);      
     });        
 },
 parsetext: function(text){
     console.log(text + ' is the text that is being returned from the promise');
     var rx = /Seite((\S+)\s+\S.*?)(?=\s*)/;
     var s =  text;
     var m = s.match(rx) || ["", ""];
     console.log(m[0] + ' is the matched text');
 }

此记录:

'...SeiteSGP0136.1 3SE7120 3SE7120-1BF00 SGP0137.1 3SE7140 3SE7140-1CD00 SGP0138.1 3SE7150 3SE7150-1BH00 SGP0136.1 is the text that is being returned from the promise'

只是为了证明 RegEx 没有被破坏:

https://jsfiddle.net/dqewqwvk/5/

感谢@async5 的建议,我首先注意到与正则表达式匹配的文本不是我想的那样,从而解决了这个问题

console.log(JSON.stringify(text));  //   '...Seite                     SGP0136.1...'    

这表明在 Seite 之后插入了额外的空格,这破坏了我的正则表达式。

我的解决方案是用空字符串替换三个以上空格的序列:

     var rx = /Seite((\S+)\s+\S.*?)(?=\s*)/;
     var s =  text.replace(/\s{3}\s+/g, '');
     var m = s.match(rx) || ["", ""];
     console.log(m[1] + ' is the matched text');