简体   繁体   English

删除缩进,但在模板文字中保留换行符

[英]Remove indentation, but keep line breaks in template literal

I have the following template literal:我有以下模板文字:

let message = `
    1 call
    Alert time: 6:00:57 PM
    `

I've tried various answers from here on SO but for the API I'm working with, none of those solutions work correctly.我已经从这里尝试了各种答案,但是对于我正在使用的 API,这些解决方案都不能正常工作。 Those solutions seem to be causing the line breaks after the opening backtick and the ending backtick to be removed along with the indentation.这些解决方案似乎导致在开始反引号和结束反引号后的换行符与缩进一起被删除。

Because this is happening the API I'm working with is treating the template literal as a single string with no line breaks.因为这种情况正在发生,所以我正在使用的 API 将模板文字视为没有换行符的单个字符串。

How can I retain all of the line breaks but remove only the indentation in front of 1 call and Alert time: 6:00:57 PM ?如何保留所有换行符,但仅删除1 callAlert time: 6:00:57 PM前面的缩进?

My expected output would be this:我预期的 output 将是这样的:


1 call
Alert time: 6:00:57 PM

A regular expression can match space characters at the beginning of the line and replace them with nothing, leaving the newlines alone:正则表达式可以匹配行首的空格字符并将它们替换为空字符,只留下换行符:

message.replace(/^ +/gm, '')

Demo:演示:

 let message = ` 1 call Alert time: 6:00:57 PM ` document.querySelector('textarea').value = message.replace(/^ +/gm, '');
 <textarea></textarea>

I would split the string by all the new lines \n , then trim the items in the array to remove the spaces then join them again with new lines \n .我会用所有新行\n split字符串,然后修剪数组中的项目以删除空格,然后用新行\n再次加入它们。 The filter is to remove the items in the split array that could be empty strings filter是删除split数组中可能是空字符串的项目

 let message = ` 1 call Alert time: 6:00:57 PM `; console.log(['', ...message.split('\n').filter(Boolean).map(str => str.trim()), ''].join('\n'));

There are, of course, a infinte ways to get to your solution.当然,有无数种方法可以找到您的解决方案。 To make only the indent of the content disappear you can use the following method with your template strings.要仅使内容的缩进消失,您可以对模板字符串使用以下方法。 This ensures that only the indent created by the template string disappears, but the basic formatting remains intact.这确保只有模板字符串创建的缩进消失,但基本格式保持不变。

 function trimIndent(strings, ...values) { const result = new Array(strings.length); result[0] = strings[0].replace(/^\n/, ''); const endIndex = strings.length - 1; result[endIndex] = strings[endIndex].replace(/\n$/, ''); var indent = result[0].match(/^\s+/g); result[0] = result[0].replace(/^\s+/, ''); for (let i = 0, m = result.length; i < m; i++) { var input = result[i]?? strings[i]; result[i] = input.replace(new RegExp('\n' + indent, "gm"), '\n'); } return result.flatMap((value, index) => { return value + (index < values.length? values[index]: '') }).join(""); } // example call: var user = { id: 10, name: "Kevin", } if (true) { console.log(trimIndent` <div> <span>ID: ${user.id}</span> <span> Username: ${user.name} </span> </div> `); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM