0

I have a regular expression which extracts some text to an array. The code works fine in the frontEnd but it doesn't work in the node.js server.

Whenever I run the code in the backend I get this error message:

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 

This is the code I'm talking about. It is a regular expression that extracts specific numbers in the "text" variable to an array.

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);

I want this script to work on the backend(node.js) as it works on the frontEnd.

7
  • 2
    You don't need new RegExp()! The /.../ notation is already a regex object ready to use. Commented Apr 27, 2019 at 13:58
  • Also when using strings you need to double escape `\`. Commented Apr 27, 2019 at 14:04
  • 1
    The second string extracted by the .split() does not match the regular expression. The $ before the 3 is not allowed. The .replace() call only gets rid of the first $ in the string, not all of them. Commented Apr 27, 2019 at 14:06
  • 1
    @Axmednuur yes and that is because the second time through the loop .match() returns null. It does so because the regular expression does not match the second string, because the attempt to remove the $ characters only removes one of them. It has nothing to do with Node. Commented Apr 27, 2019 at 14:17
  • 1
    var reso = text.replace(/\$/g, "") will replace all of them :) Commented Apr 27, 2019 at 14:29

2 Answers 2

1

You have to define:

var regularExpression = new RegExp("id:\s+([0-9]+).+Waxaad\s+([0-9]+).+[^\(]+\(([0-9]+)\)\s+Tar:\s+([0-9\/\s:]+)", "i");

Or

var regularExpression = /id:\s+([0-9]+).+Waxaad\s+([0-9]+).+[^\(]+\(([0-9]+)\)\s+Tar:\s+([0-9\/\s:]+)/i;
Sign up to request clarification or add additional context in comments.

2 Comments

I tried your answer. It still can't find the item[] object. It displays this error :- TypeError: Cannot read property '1' of null
@Axmednuur: Because your regex doesn't match your string. For example, Waxaad\s+([0-9]+) doesn't match Waxaad $55. Before using item[1] test if item is a non empty array.
1

I figured out the problem.

  1. I removed all the '$' signs by using this script var reso = text.split('$').join("");
  2. I removed the new RegExp.

And then it worked fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.