我正在读取文件的内容,并希望以一种优雅的方式显示它。最初,我使用split('\n')
分割文本,但是在该文件中的某处有多行文本,并且该文本也正在使用我不希望的这种方式进行分割。所以我找到了一个窍门,我要用日期分割文本,请看下面的例子,并建议我该怎么做?
12/10/18, 10:33 AM - User1: Here is a line of code 12/10/18, 10:33 AM - User2: Here is multi-line of code written in this line
如您在上面的示例中看到的那样,每一行都以12/10/18
之类的日期开头。所以我想在日期之前分割行。这样吧
12/10/18, 10:33 AM - User1: Here is a line of code
12/10/18, 10:33 AM - User2: Here is multi-line of code written in this line.
非常感谢您的帮助。预先感谢。
答案 0 :(得分:2)
您可以使用日期模式进行拆分:
var str = "12/10/18, 10:33 AM - User1: Here is a line of code 12/10/18, 10:33 AM - User2: Here is multi-line of code written in this line";
var x = str.split(/(\d{2}\/\d{2}\/\d{2})/);
for (i=1; i< x.length; i += 2) {
console.log(x[i] + x[i+1]);
}
答案 1 :(得分:1)
您可以这样做:
const str = '12/10/18, 10:33 AM - User1: Here is a line of code 12/10/18, 10:33 AM - User2: Here is multi-line of code written in this line';
const regex = /(?=[0|1]\d{1}\/[0|1|2]\d{1}\/[18|19|20])/g;
const result = str.split(regex);
console.log(result);
答案 2 :(得分:1)
如果您希望包含两个部分,则可以使用“日期喜欢”模式在split上positive lookahead(请注意,这不会验证日期本身):
(?=\d{2}\/\d{2}\/\d{2})
例如:
var str = "12/10/18, 10:33 AM - User1: Here is a line of code 12/10/18, 10:33 AM - User2: Here is multi-line of code written in this line";
var parts = str.split(/(?=\d{2}\/\d{2}\/\d{2})/);
console.log(parts);
答案 3 :(得分:1)
->Where('l.oficina = :data1')
->andWhere('l.area = :data2')
-> setParameter('data1', $data1)
-> setParameter('data2', $data2)