- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我使用 PDF.js 从 PDF 中获取文本,然后使用正则表达式对其进行解析。 parsetext
函数采用一个由 Promise 返回的 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'
}
m[1]
应该返回一个很长的字符串。
删除了涉及在未使用捕获组时尝试从 String.match() 获取捕获组的困惑情况 - 主要问题仍未解决,因此这不是重复的。
问题可能出在哪里?正则表达式看起来很好,所以我只能想象这是 gettext
在运行 parsetext
之前没有返回完整字符串的结果。但这不就是 promise 所保证的吗?
我认为这不是没有返回值的问题,因为倒数第二个 promise 返回一个字符串。为了证明这一点,我添加了一个 console.log 来显示返回的内容:
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*\2)/;
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'
只是为了表明正则表达式没有损坏:
最佳答案
感谢@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');
关于javascript - 尝试将 RegEx 与 Promise 返回的文本匹配 - 获取空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43994432/
我正在尝试用 Java 构建一个字符串,该字符串的长度最多为 3,最少为 1。 我正在根据整数数组的内容构建字符串,如果数组的内容为 -1,我想在字符串中输出一个空字符。否则字符串将包含整数的字符版本
我有一个类,其中有一个方法可以在字符串中包含 NUL 字符的情况下终止程序。具体表达是这样的: stringVar.indexOf('\u0000') < 0 这个字符串是通过 Scanner 从用户
我有一个 wchar_t 数组。我需要在数组中的特定位置添加一个 unicode 空字符。 wchar_t var1[100]; var1[79] = '\u0000'; 我尝试了上面的方法,但出现以
好吧,这听起来可能是重复的,但我已经尝试了所有可能性,例如 str.strip()、str.rstrip()、str.splitline (),还 if-else 检查像: if str is not
System.out.println("-----------------------------------------------------------"); System.out.pr
我有一个奇怪的问题。我从公司内部的许多不同应用程序接收数据,并将这些数据显示在网站上。根据发送数据的系统,数据本身可能在字符串中包含一些奇怪的字符。我的问题是我有一个用户可以搜索以允许其中包含此数据的
我遇到了 aSSL ,这似乎有几年历史了,想知道是否有人有其他“安全”AJAX 连接代码示例?显然,这不如使用 SSL 证书安全,但使用 null character SSL在那里进行攻击(最近针对
我有一个类似于以下内容的 pyspark 数据框: df = sql_context.createDataFrame([ Row(a=3, b=[4,5,6],c=[10,11,12], d='b
我有以下要执行的查询: MyModel.objects.annotate(current_name=Coalesce('nickname', 'name')).order_by('current_na
每当 rails 变量等于 nil(或者实际上每当我使用 rails 代码(参见第 3 个代码示例))时,我的 html 中就会得到一串空字符。 new.html.haml %h1.editable.
我是一名优秀的程序员,十分优秀!