+ 运算符覆盖而不是连接 Javascript 中的字符串
+ operator overwrites instead of concatenating strings in Javascript
我正在尝试从字符串参数中获取行并将它们复制到新的字符串变量中。只要新行不匹配特定的正则表达式,就会执行此逻辑。
出于某种原因(我不知道)输出是我所期望的....
这是代码:
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
但是当我 运行 代码时...我得到这个 'weird' 输出:
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
我认为你的打印有误。你需要console.log('new x is ' + x + '\n)
。
请检查 console.log
documentation.
我试过你的代码——稍微修改过的版本(使用简单的节点 JS 项目):
function matchRegexExp(log) {
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);
}
function createString(string1) {
console.log("Value of string1 is:");
console.log(string1);
console.log()
let i = 0;
let readLine = string1.split('\n');
let x ='';
while (i < readLine.length) {
if (matchRegexExp(readLine[i]) === null) {
console.log('regex expression returns... ' + 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;
}
const testString = `ana
has
apples
and
oranges
2019-01-01 11:11:11 INFO ThisApp - Started App`;
createString(testString);
我得到了这个打印输出:
Value of string1 is:
ana
has
apples
and
oranges
2019-01-01 11:11:11 INFO ThisApp - Started App
regex expression returns... null
current line content is... ana
x is =
new x is ana
regex expression returns... null
current line content is... has
x is = ana
new x is anahas
regex expression returns... null
current line content is... apples
x is = anahas
new x is anahasapples
regex expression returns... null
current line content is... and
x is = anahasapples
new x is anahasapplesand
regex expression returns... null
current line content is... oranges
x is = anahasapplesand
new x is anahasapplesand oranges
final x is = anahasapplesand oranges
请注意,我使用的是 CRLF 文件。
这是您想要的结果吗?
在我看来这像是一个 CRLF 问题。您正在拆分 '\n' 上的输入字符串。但是,如果输入字符串有行分隔符 '\r\n'(因为来自 Windows 的数据很可能),那么你最终会得到 x 包含如下内容:
ana\rhas\rapples\r\rand \roranges
打印出来会很奇怪('\r' 会将光标重置到行首)。
尝试在“\r\n”上拆分输入字符串,看看是否有帮助。
或者,当您构建 'x' 时,您可以再次添加 '\n' 以生成 multi-line 字符串:
x = x + readLine[i] + '\n';
我正在尝试从字符串参数中获取行并将它们复制到新的字符串变量中。只要新行不匹配特定的正则表达式,就会执行此逻辑。
出于某种原因(我不知道)输出是我所期望的....
这是代码:
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
但是当我 运行 代码时...我得到这个 'weird' 输出:
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
我认为你的打印有误。你需要console.log('new x is ' + x + '\n)
。
请检查 console.log
documentation.
我试过你的代码——稍微修改过的版本(使用简单的节点 JS 项目):
function matchRegexExp(log) {
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);
}
function createString(string1) {
console.log("Value of string1 is:");
console.log(string1);
console.log()
let i = 0;
let readLine = string1.split('\n');
let x ='';
while (i < readLine.length) {
if (matchRegexExp(readLine[i]) === null) {
console.log('regex expression returns... ' + 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;
}
const testString = `ana
has
apples
and
oranges
2019-01-01 11:11:11 INFO ThisApp - Started App`;
createString(testString);
我得到了这个打印输出:
Value of string1 is:
ana
has
apples
and
oranges
2019-01-01 11:11:11 INFO ThisApp - Started App
regex expression returns... null
current line content is... ana
x is =
new x is ana
regex expression returns... null
current line content is... has
x is = ana
new x is anahas
regex expression returns... null
current line content is... apples
x is = anahas
new x is anahasapples
regex expression returns... null
current line content is... and
x is = anahasapples
new x is anahasapplesand
regex expression returns... null
current line content is... oranges
x is = anahasapplesand
new x is anahasapplesand oranges
final x is = anahasapplesand oranges
请注意,我使用的是 CRLF 文件。
这是您想要的结果吗?
在我看来这像是一个 CRLF 问题。您正在拆分 '\n' 上的输入字符串。但是,如果输入字符串有行分隔符 '\r\n'(因为来自 Windows 的数据很可能),那么你最终会得到 x 包含如下内容:
ana\rhas\rapples\r\rand \roranges
打印出来会很奇怪('\r' 会将光标重置到行首)。
尝试在“\r\n”上拆分输入字符串,看看是否有帮助。
或者,当您构建 'x' 时,您可以再次添加 '\n' 以生成 multi-line 字符串:
x = x + readLine[i] + '\n';