【发布时间】:2019-07-04 12:20:55
【问题描述】:
我正在尝试从字符串参数中获取行并将它们复制到新的字符串变量中。只要新行与特定的正则表达式不匹配,此逻辑就会执行。
由于某种原因(我不知道),输出是我所期望的......
这是代码:
matchRegexExp(log: string) {
let regexString = /(?:\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})[\s\S]+?((?=\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})|$)/g;
return log.match(regexString);
}
}
private createString(string1: string) {
let i: number = 0;
let readLine: string[] = string1.split('\n');
let x: string ='';
while (this.matchRegexExp(readLine[i]) == null) {
console.log('regex expression returns... ' + this.matchRegexExp(readLine[i]));
console.log('current line content is... ', readLine[i]);
console.log('x is = ', x);
x = x + readLine[i];
console.log('new x is ',x , '\n');
i++;
}
console.log('final x is = ', x, '\n');
return x;
}
这是来自 string1 的数据:
ana
has
apples
and
oranges
2019-01-01 11:11:11 INFO ThisApp - Started App
与我的正则表达式不匹配且必须复制到字符串中的行是:
ana
has
apples
and
oranges
但是当我运行代码时......我得到了这个“奇怪”的输出:
regex expression returns... null
current line content is... ana
x is =
ew x is ana
regex expression returns... null
current line content is... has
x is = ana
as x is ana
regex expression returns... null
current line content is... apples
hass = ana
pplesis ana
regex expression returns... null
current line content is... and
apples ana
nd esis ana
regex expression returns... null
current line content is... oranges
and es ana
oranges ana
orangess = ana
【问题讨论】:
-
什么是
this.matchRegexExp? -
你运行它的环境是什么?代码看起来基本正确,见typescriptlang.org/play/index.html#code/…
-
@adiga 我已经用 'matchRegexExp' 方法更新了代码
-
@TPReal 用 typescript 编写并在 VS Code 中使用 node v.10.15.3 运行的代码
-
如果有这个代码的完全可运行版本会很棒。
let regexString = *some regex expression*;不是有效的 js 并且大括号实际上不匹配
标签: javascript node.js typescript string-concatenation