Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 防止ES6模板字符串中出现换行_Javascript_Ecmascript 6_Template Strings - Fatal编程技术网

Javascript 防止ES6模板字符串中出现换行

Javascript 防止ES6模板字符串中出现换行,javascript,ecmascript-6,template-strings,Javascript,Ecmascript 6,Template Strings,ESLint:第403行超出了最大行长度120(最大长度) 我有一个长字符串,我使用ES6模板字符串构建它,但我希望它没有换行符: var string = `Let me be the 'throws Exception’ to your 'public static void main (String[] args)’. I will accept whatever you give me my ${love}.` console.log(string); 结

ESLint:第403行超出了最大行长度120(最大长度)

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

var string = `Let me be the 'throws Exception’ to your 'public static void 
              main (String[] args)’. I will accept whatever you give me my ${love}.`
console.log(string);
结果:

 Let me be the 'throws Exception’ to your 'public static void 
 main (String[] args)’. I will accept whatever you give me xxx.
我的期望:

Let me be the 'throws Exception’ to your 'public static void main (String[] args)’. I will accept whatever you give me xxx.
要求

  • 我无法禁用eslint规则,因为强制执行是必要的

  • 我不能将数据放在单独的文件中,因为数据是动态的

  • 我无法连接多个较短的字符串,因为这太麻烦了


  • 这是预期的行为。模板文字解决的一个重要问题是:

    在源代码中插入的任何新行字符都是模板文字的一部分

    如果字符串需要进一步处理,可以使用其他JS功能,如正则表达式:

    var string = `Let me be the 'throws Exception’ to your 'public static void 
                  main (String[] args)’. I will accept whatever you give me.`
                  .replace(/[\n\r]+ */g, ' ');
    
    是用于转换模板文字的内置函数。可以使用标记函数为模板文本提供自定义行为。应该注意的是,
    String.raw
    在处理方式上与默认模板转换器不同。如果在字符串中使用它们,则应使用或类似的辅助函数对它们进行额外处理

    function singleLine(strsObj, ...values) {
      const strs = strsObj.raw
      .map(str => str.replace(/[\n\r]+ */g, ' '))
      .map(unescapeSpecialChars);
      return String.raw(
        {raw: strs },
        ...values
      );
    }
    
    
    var string = singleLine`Let me be the 'throws Exception’ to your 'public static void 
                  main (String[] args)’. I will accept whatever you give me.`;
    

    如果您的问题只是EsLint错误,您可以使用此功能忽略此特定行的问题:
    /*EsLint disable max len*/

    在我看来,诚实是最好的方法,因为你没有给额外的复杂性


    如果您开始使用正则表达式或连接,您正在通过不使用连接来更改模板字符串的用途…

    同时使用字符串连接:

    var string=`abc`+`def`;
    console.log(string);
    
    收益率:


    abcdef

    达到目的的一个好方法是加入字符串数组:

    var string = [
      `Let me be the 'throws Exception’ to your 'public static void`,
      `main (String[] args)’. I will accept whatever you give me my ${love}.`
    ].join(' ');
    

    如果不需要换行符,请不要写入任何?ESLint:403行超过了最大行长度120(最大长度)
    myStr.replace(“\n”,”)
    Ragarding
    c
    ,它很甜,但并不像你想象的那样有效。可以使用自定义标记函数使其转换字符串。查看更新。您可以更改规则吗?它有一个选项。我如何才能为一行启用它?/*eslint disable max len/line/eslint enable max len*/??每当我需要执行以下操作时,这看起来像是额外添加了两行this@LokeshAgrawal我如何通过阅读文档为一行启用它?语法是
    //eslint disable line max len
    。不,
    String.raw
    表示其他意思。考虑“代码>”\“代码/代码>和代码>字符串之间的区别。我知道它在模板字符串中的工作原理是这样的,所以是的,当直接调用它时,它的行为也是这样是有道理的。你知道如何通过偶然的机会可靠地重新创建默认转换器吗?@LokeshAgrawal是的,
    oneLine
    与答案中列出的非常接近。这取决于是否需要进一步定制,例如应如何处理多个换行符。