替换字符串数组中的正则表达式字符串
2019-01-11
67
我正在为高中生创建一个基于 Web 的会计应用程序,供他们练习使用。我的
transactionListArray
包含我在 JS 代码中后台随机生成的所有交易。
transactionListArray
包含某些字符,包括第一个字符,即
date
,它是一个
integer
,后面跟着一个
.
(例如:
10.
或
12.
等)。日期后面有一个句子,它创建了会计交易的措辞、帐户名称、付款方式和其他各种内容。
基本交易会产生以下输出:
27. Trusted Traders purchased trading stock to the value of R108756.
我到处找过了,但仍然找不到适合我的解决方案。
几天来,我一直面临的问题是试图弄清楚如何使用正则表达式
match
关键字返回字符串。当我尝试将
currentString
与
nextString
(数组中的下一个值)进行匹配时,问题就出现了。
见下文:
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
};
上述操作并未产生我期望的输出。
currentDate
和
nextDate
行中显示的错误为:
TypeError: Cannot read property '0' of null
此问题令人困惑,因为我已检查当前迭代和下一次迭代的值,但它并未返回我的正则表达式字符串。
例如,我期望的是:
currentDate[0] = '11.';
nextDate[0] = '10.';
然后,当
currentString
和
NextString
相等时,我想替换
nextString
。
像这样:
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]
}
}
};
我在我的
transactionListArray
上调用这样的函数:
replaceDateWithBlankSpace(transactionListArray);
2个回答
如果要改变原始数组,可以这样做:
const arr = [
'25. Trusted Traders purchased trading stock to the value of R138756.',
'26. Trusted Traders purchased trading stock to the value of R432756.',
'26. Trusted Traders purchased trading stock to the value of R108756.',
'28. Trusted Traders purchased trading stock to the value of R333756.',
];
const replaceDateWithBlankSpace = array => {
const length = array.length - 1;
const regex = /^\d+\./;
for (let i = 0; i < length; i++) {
const currentString = array[i];
const nextString = array[i + 1];
const currentDate = currentString.match(regex);
const nextDate = nextString.match(regex);
if (currentDate && nextDate && currentDate[0] === nextDate[0]) {
array[i + 1] = array[i + 1].replace(regex, ' ');
}
}
};
replaceDateWithBlankSpace(arr);
console.log(arr);
Ray Chan
2019-01-11
好的,我可能误解了你,所以请检查这个解决方案是否符合你的需求:
// I define the transactions array that you generate randomly:
const transactionListArray = [
'5. Lorem ipsum dolor sit amet',
'5. consectetur adipiscing elit',
'6. Praesent aliquet ut ex eget mattis',
'7. Donec fermentum sodales quam',
'7. quis vestibulum justo posuere quis',
'8. Fusce tristique accumsan pretium',
'9. Quisque ante metus',
'9. vestibulum sed lacinia sed',
'9. elementum ac nunc',
'10. Suspendisse vel sollicitudin neque',
'10. vel sagittis quam'
];
function replaceDateWithBlankSpace (array) {
return array.map((transaction, index, array) => {
const regex = /^(\d+\.)(.*)$/;
let lastDate = array[index-1] ? array[index-1].replace(regex, '$1') : '';
let thisDate = transaction.replace(regex, '$1');
if (lastDate === thisDate) {
return transaction.replace(regex, '$1').replace(/./g,' ')
+ transaction.replace(regex, '$2');
} else {
return transaction;
}
});
}
console.log(replaceDateWithBlankSpace(transactionListArray));
dquijada
2019-01-11