我有一个正则表达式,它将一些文本提取到数组中。该代码在frontEnd中可以正常工作,但在node.js服务器中不起作用。
每当我在后端运行代码时,都会收到此错误消息:
TypeError: Cannot read property '1' of null
at C:\Users\PureTech\master\app\server\routes.js:26:11
at Layer.handle [as handle_request] (C:\Users\PureTech\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\PureTech\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (C:\Users\PureTech\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\PureTech\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\PureTech\node_modules\express\lib\router\index.js:277:22
at Function.process_params (C:\Users\PureTech\node_modules\express\lib\router\index.js:330:12)
at next (C:\Users\PureTech\node_modules\express\lib\router\index.js:271:10)
at C:\Users\PureTech\node_modules\express-session\index.js:433:7
at C:\Users\PureTech\node_modules\connect-mongo\lib\connect-mongo.js:305:11
这是我正在谈论的代码。这是一个正则表达式,可将“文本”变量中的特定数字提取到数组中。
var text = '[Extracted] id: 194805284, Waxaad $55 ka heshay MAXAMED CABDILAAHI JAAMAC SAALAX (252906152669) Tar: 15/04/19 08:44:40, Haraagaagu waa $1,042.7[Extracted] id: 193537533, Waxaad $3 ka heshay ABDULKADIR ABDIDAHIR FARAH (907794804) Tar: 14/04/19 10:15:32, Haraagaagu waa $59.17';
var reso = text.replace("$", "");
var textArray = reso.split('[Extracted]');
var regularExpression = new RegExp(/id:\s+([0-9]+).+Waxaad\s+([0-9]+).+[^\(]+\(([0-9]+)\)\s+Tar:\s+([0-9\/\s:]+)/i);
var output = [];
var item;
for(var i = 1; i < textArray.length; i++){
item = textArray[i].match(regularExpression);
output.push({
id: item[1].trim(),
amount: item[2].trim(),
time: item[3].trim(),
number: item[4].trim()
});
}
console.log(output);
我希望此脚本在后端(node.js)上工作,因为它在前端上工作。
答案 0 :(得分:2)
您必须定义:
var regularExpression = new RegExp("id:\s+([0-9]+).+Waxaad\s+([0-9]+).+[^\(]+\(([0-9]+)\)\s+Tar:\s+([0-9\/\s:]+)", "i");
或
var regularExpression = /id:\s+([0-9]+).+Waxaad\s+([0-9]+).+[^\(]+\(([0-9]+)\)\s+Tar:\s+([0-9\/\s:]+)/i;
答案 1 :(得分:1)
我发现了问题所在。
Db.DbDataSet.Tables["costallocations"]
.AsEnumerable()
.Where(Row => Row.Field<long>("callocid") == OldCostAllocationId)[0]["newid"]
然后工作正常。