Javascript 模板字符串ES6防止换行

Javascript 模板字符串ES6防止换行,javascript,ecmascript-6,template-strings,Javascript,Ecmascript 6,Template Strings,我有一个长字符串,我使用ES6模板字符串构建它,但我希望它没有换行符: var string = `As all string substitutions in Template Strings are JavaScript expressions, we can substitute a lot more than variable names. For example, below we can use expression inter

我有一个长字符串,我使用ES6模板字符串构建它,但我希望它没有换行符:

var string = `As all string substitutions in Template Strings are JavaScript
              expressions, we can substitute a lot more than variable names.
              For example, below we can use expression interpolation to 
              embed for some readable inline math:`

console.log(string);
结果:

As all string substitutions in Template Strings are JavaScript
expressions, we can substitute a lot more than variable names.
For example, below we can use expression interpolation to
embed for some readable inline math:
我的期望:

As all string substitutions in Template Strings are JavaScript expressions, we can substitute a lot more than variable names. For example, below we can use expression interpolation to embed for some readable inline math:

换行符是换行符。。。如果您手动生成它们,我发现您可以在运行时得到它们

顺便说一句,我发现目前有三种解决办法:

  • 将IDE或代码编辑器配置为执行换行操作,以便在不需要换行的情况下不需要在代码中添加换行符:如果每个代码语句超过配置的最大字符数,编辑器会将代码分为两行或更多行

  • 使用
    字符串删除换行符。原型。替换

  • var string=`因为模板字符串中的所有字符串替换都是JavaScript 表达式,我们可以替换比变量名多得多的内容。 例如,下面我们可以使用表达式插值 嵌入一些可读的内联数学:`.replace(/\n/gm,“”); 注意:这里您正在运行一个函数运行时来格式化构建时代码,这可能看起来像反模式,并且会影响性能

  • 使用串联执行以下代码行中断:
  • var string=`因为模板字符串中的所有字符串替换都是JavaScript` +'表达式,我们可以替换的远不止变量名` +'例如,下面我们可以使用表达式插值来` +`embed for some readable inline math:`;
    就我而言,我会选择#1选项。

    我个人更喜欢加入阵列的外观:

    var字符串=[
    `由于模板字符串中的所有字符串替换都是JavaScript`,
    `表达式,我们可以替换比变量名多得多的内容,
    `例如,下面我们可以使用表达式插值来`,
    `嵌入一些可读的内联数学:`
    ].加入(“”);
    
    如果您有ES6,您可以使用标签。例如:

    通过以下方式安装:

    npm install common-tags --save
    
    要求通过:

    const stripIndent = require('common-tags/lib/stripIndent')
    
    用作:

    stripIndent`
      As all string substitutions in Template Strings are JavaScript
      expressions, we can substitute a lot more than variable names.
      For example, below we can use expression interpolation to
      embed for some readable inline math:        
    `
    
    编辑:如评论中所述,您可能需要为所需结果选择:
    const-oneLine=require('common-tags/lib/oneLine')
    标记

    关于上述通用标签链接以及

    的更多信息这太疯狂了

    这里几乎每一个答案都建议运行函数运行时,以便很好地格式化,构建时格式错误的文本oO我是唯一一个被这一事实震惊的人,尤其是性能影响

    正如@dandavis所述,(顺便说一句,它也是unixshell脚本的历史性解决方案)是用转义字符转义回车:
    \

    `foo \
    bar` === 'foo bar'
    
    过程中简单、高效、正式、易读且类似外壳

    • 或者将IDE配置为进行换行,并使用模板字符串1-liner,就像在第一个代码段中一样

    • 在换行之前使用
      \
      转义文字字符

      示例:

      const string=`1行\
      第二行\
      第三行`;
      
      但这并不能避免空间对齐问题

    • 或者使用带有“+”的旧式ES5连接

      示例:

      const string='1st line'+
      “第二行”+
      “第三线”;
      
    • 将hack与模板空字符串var${'}一起使用:

      示例:

      const string=`1st line${'
      }第二行${“
      }第三行`;
      
    第一种方法更好,因为:

    • 减少符号(尺寸方面)
    • 无运行时操作(性能方面)

    在ES6中,我更喜欢结合使用两个最重要的答案(
    \
    .replace()
    )。将replace函数与转义字符相结合的好处是,您可以使代码块的格式与代码的其余部分保持一致

    /\s{2}/g
    是一个正则表达式,用于选择两个或多个背靠背空间的任何实例

    const myMessage=`Lorem ipsum door sit amet,constetetur adipsicing elit。Duis id urna\
    舌苔。静脉前庭前叶悬吊术。Donec imperdiet ante odio\
    nec malesuada diam tristique eget。Lorem ipsum dolor sit amet,Concertetur Adipising\
    精英。秃鹫岛上的克拉斯`
    .替换(/\s{2,}/g,“”);
    
    这将输出一个普通的、未断开的字符串


    “我的知识是,我的知识是,我的知识是,我的知识是,我的知识是,我的知识是,我的知识是,我的知识是,我的知识是,我的知识是,我的知识是,我的知识是,我的知识是,我的知识是,我的知识是,我的知识是,我的知识是,我的知识是,我的知识是,我的知识是,我的知识是,我的知识是,我的知识是,我的知识是,我的知识是。”,您可以使用
    \s+
    正则表达式将空白运行替换为单个空格,这样它既可以用于换行,也可以用于缩进。因此,在您的情况下,只需在末尾添加
    。替换(/\s+/gm',)

    var string = `As all string substitutions in Template Strings are JavaScript
                  expressions, we can substitute a lot more than variable names.
                  For example, below we can use expression interpolation to 
                  embed for some readable inline math:`.replace(/\s+/gm, ' ');
    
    console.log(string);
    

    用斜杠“\”转义换行符,它将在输出中消失,即使在模板字符串中也是如此。第二个示例的近似重复项不能正常工作。线条之间有很大的间隙。@dannyid你说的大间隙是什么意思?它包含的字符是否比换行符多?@MatíasFidemraizer,将#2复制/粘贴到控制台中,然后运行
    string
    。您可以获得字符串,但
    Javascript
    表达式之间有许多空格,例如
    名称。
    等等。您可能无法在浏览器中看到它,因为浏览器通常会删除额外的空格,但实际的底层数据是不正确的。@dannyid哦,我明白了,顺便说一句,这是字符串本身的问题,不是方法。无论如何,我将更新我的答案,以解决库的+1问题,并提及ES6标记。但是,在这种情况下,我认为OP需要来自
    公共标记库的标记
    
    `foo \
    bar` === 'foo bar'
    
    var string = `As all string substitutions in Template Strings are JavaScript
                  expressions, we can substitute a lot more than variable names.
                  For example, below we can use expression interpolation to 
                  embed for some readable inline math:`.replace(/\s+/gm, ' ');
    
    console.log(string);