I am creating a web-based accounting app for high school kids to use as practice. My transactionListArray contains all of my transactions that have been randomly generated behind the scenes in my JS code. The transactionListArray contains certain characters including the first character which is the date as an integer with a . following it (for example: 10. or 12. etc). After the date there is a sentence which creates the accounting transactions' wording, account names, payment methods and various other things.
A basic transaction produces this output:
27. Trusted Traders purchased trading stock to the value of R108756.
I have looked everywhere and I still cannot find the solution that suits my liking.
The problem I have been faced with for several days now, is trying to figure out how to use the regex match keyword to return a string. The issue comes in when I try match the currentString with the nextString which is the next value in the array.
See below:
let length = array.length-1;
for (let i = 0; i < length; i++) {
let regex = /d+\./; // this returns the value of the first number(the date) and the "." symbol
let currentString = array[i];
let nextString = array[i+1];
let currentDate = currentString.match(regex); // errors
let nextDate = nextString.match(regex); // errors
};
This above doesn't produce the output I am expecting. The error as stated on currentDate and nextDate lines says:
TypeError: Cannot read property '0' of null
This issue is confusing because I have checked the current iteration and the next iterations values but it doesn't return my regex string.
I'm expecting this for example:
currentDate[0] = '11.';
nextDate[0] = '10.';
I then want to replace the nextString when the currentString and NextString are equal.
Like this:
let replaceDateWithBlankSpace = (array) => {
let length = array.length-1;
for (let i = 0; i < length; i++) {
let regex = /d+\./;
let currentString = array[i];
let nextString = array[i+1];
let currentDate = currentString.match(regex); // TypeError: Cannot read property '0' of null
let nextDate = nextString.match(regex); // TypeError: Cannot read property '0' of null
if (currentDate[0] === nextDate[0]) { // checking if both are equal
nextString.replace(regex, " "); // and then replacing the string regex that was calculated with blank space at array[i+1]
}
}
};
I call the function like this on my transactionListArray:
replaceDateWithBlankSpace(transactionListArray);
let regex = /d+\./;you mean/\d+./- an escapeddfor digit, rather than than the literal characterd..replace()line does replace the date. Then discards the newly created string as it's neither assigned to anything, nor returned.array[i+1] = nextString.replace(regex, " ");