【问题标题】:+ operator overwrites instead of concatenating strings in Javascript+ 运算符覆盖而不是连接 Javascript 中的字符串
【发布时间】: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


【解决方案1】:

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

ana\rhas\rapples\r\rand \roranges

打印出来时看起来很奇怪('\r' 会将光标重置到行首)。

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

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

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

【讨论】:

    【解决方案2】:

    我认为您的打印错误。你需要console.log('new x is ' + x + '\n)。 请查看console.logdocumentation。

    我已经尝试过你的代码 - 稍微修改的版本(使用简单的节点 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 文件。

    这是你想要的结果吗?

    【讨论】:

    • 相同的结果...一开始就按照你的方式尝试
    猜你喜欢
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 2020-07-23
    • 2019-01-09
    • 2010-09-21
    相关资源
    最近更新 更多