Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试将RegEx与promise返回的文本匹配--获取空字符串_Javascript_Node.js_Regex_Promise_Pdf.js - Fatal编程技术网

Javascript 尝试将RegEx与promise返回的文本匹配--获取空字符串

Javascript 尝试将RegEx与promise返回的文本匹配--获取空字符串,javascript,node.js,regex,promise,pdf.js,Javascript,Node.js,Regex,Promise,Pdf.js,我使用PDF.js从PDF中获取文本,然后用正则表达式解析。parsetext函数接受由承诺返回的text参数: gettext: function(url){ var self = this; var data = url; console.log('attempting to get text'); return pdfjs.getDocument(data).then(function(pdf) { var pages = [];

我使用PDF.js从PDF中获取文本,然后用正则表达式解析。
parsetext
函数接受由承诺返回的
text
参数:

 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*\2)/;
     var s = text;
     var m = s.match(rx) || ["", ""];
     console.log(m[1] + ' is the matched text');  //   returns '  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'
只是为了证明正则表达式没有被破坏:


多亏@async5的建议,我才得以解决这个问题,因为我首先注意到与正则表达式匹配的文本并不是我想象的那样

console.log(JSON.stringify(text));  //   '...Seite                     SGP0136.1...'    
这表明在
Seite
之后插入了额外的空格,这破坏了我的正则表达式

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

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

@JaromandaX没有。
then(函数(页面){returnpages.join(“\r\n”)})返回文本,然后发送到
self.parsetext
?当您尝试硬编码时,您使用的是一个不分组任何内容的regexp。regexp匹配成功,但由于结果仅包含从
m[1]
获得的
undefined
的完整匹配,因为
m
只有一个item@DavidJ.,因为在该正则表达式(即括号)中有一个捕获组:
((\S+)\S+\S.*)
。该子表达式捕获的部分存储在
m[1]
中。在这种情况下,还有一个
m[2]
,它对应于嵌套组
(\S+)
所匹配的内容。@trincot:我建议您删除它好吗?问题不是异步编程的问题。OP甚至记录/解释说文本正在进入
parsetext
函数。PDF可能包含其他控制字符,请使用
JSON.stringify(text)
记录内容
     var rx = /Seite((\S+)\s+\S.*?)(?=\s*\2)/;
     var s =  text.replace(/\s{3}\s+/g, '');
     var m = s.match(rx) || ["", ""];
     console.log(m[1] + ' is the matched text');