+运算符覆盖而不是串联Javascript中的字符串

时间:2019-07-04 12:20:55

标签: javascript node.js typescript string-concatenation

我正在尝试从字符串参数获取行并将其复制到新的字符串变量中。只要新行与特定的正则表达式不匹配,该逻辑就会执行。

由于某种原因(我不知道),输出是我所期望的。...

这是代码:

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;
    }

这是来自字符串1的数据:

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

2 个答案:

答案 0 :(得分:1)

在我看来,这就像一个CRLF问题。您正在'\ n'上分割输入字符串。但是,如果输入字符串具有行分隔符“ \ r \ n”(因为可能来自Windows的数据),那么最终将得到x包含以下内容:

ana\rhas\rapples\r\rand \roranges

在打印时看起来会很奇怪('\ r'会将光标重置到行的开头)。

尝试在'\ r \ n'上分割输入字符串,看看是否有帮助。

或者,当您构建'x'时,可以再次添加'\ n'以产生多行字符串:

x = x + readLine[i] + '\n';

答案 1 :(得分:0)

我认为您的打印错误。您需要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文件。

这是您想要的结果吗?