Javascript删除字符串中的空白`

Javascript删除字符串中的空白`,javascript,html,string,Javascript,Html,String,我认为JavaScript在编写大字符串时非常有用。我的问题是,它考虑了JavaScript中它前面的空白量。因此,如果您的字符串在JavaScript中缩进,那么它也将在字符串中缩进。有没有办法摆脱这个 因为在这样的示例中,我希望html标记与字符串中的左侧对齐,但我不希望使其与JavaScript中的左侧对齐。检查console.log如何更好地理解它 MyFunction=function(){ console.log(` `); } MyFunction()你是说空格 MyFunct

我认为JavaScript在编写大字符串时非常有用。我的问题是,它考虑了JavaScript中它前面的空白量。因此,如果您的字符串在JavaScript中缩进,那么它也将在字符串中缩进。有没有办法摆脱这个

因为在这样的示例中,我希望html标记与字符串中的左侧对齐,但我不希望使其与JavaScript中的左侧对齐。检查console.log如何更好地理解它

MyFunction=function(){
console.log(`
`);
}
MyFunction()你是说空格

MyFunction=function(){
console.log(`
`.trim());
}
MyFunction()你是说空格

MyFunction=function(){
console.log(`
`.trim());
}

MyFunction()您可以按
\n
拆分文本,在第一行中找到缩进并将其从行的其余部分删除,然后再次合并

MyFunction=function(){
让缩进=”;
console.log(`
`.split(“\n”).reduce((行,行)=>{
如果(!indent.length&&line.trim()){
缩进=行。匹配(/^(\s+/)[0];
}
行。推(行。替换(缩进“”);
回流线;
},[])。筛选器(line=>line.length.join(“\n”);
};

MyFunction()您可以按
\n
拆分文本,在第一行中找到缩进并将其从行的其余部分删除,然后再次合并

MyFunction=function(){
让缩进=”;
console.log(`
`.split(“\n”).reduce((行,行)=>{
如果(!indent.length&&line.trim()){
缩进=行。匹配(/^(\s+/)[0];
}
行。推(行。替换(缩进“”);
回流线;
},[])。筛选器(line=>line.length.join(“\n”);
};

MyFunction()我将执行以下操作:

MyFunction = function() {
  /*however many spaces you want to remove from the front*/
  const spaces = '    '; // four spaces
  /* or */
  const spacesRegex = /\s+/g;
  let str = '';
  const html = `
    <html>
      <head>
      </head>
      <body>
      </body>
    </html>
  `.split('\n')
  .forEach(line=>{
    str += line.replace(spaces, '') + '\n';
  });
  console.log(str);

}
MyFunction=function(){
/*要从前面删除的空间数量*/
常量空间=“”;//四个空间
/*或*/
const spacesRegex=/\s+/g;
设str='';
常量html=`
`.split(“\n”)
.forEach(行=>{
str+=line.replace(空格、)+'\n';
});
console.log(str);
}

我将执行以下操作:

MyFunction = function() {
  /*however many spaces you want to remove from the front*/
  const spaces = '    '; // four spaces
  /* or */
  const spacesRegex = /\s+/g;
  let str = '';
  const html = `
    <html>
      <head>
      </head>
      <body>
      </body>
    </html>
  `.split('\n')
  .forEach(line=>{
    str += line.replace(spaces, '') + '\n';
  });
  console.log(str);

}
MyFunction=function(){
/*要从前面删除的空间数量*/
常量空间=“”;//四个空间
/*或*/
const spacesRegex=/\s+/g;
设str='';
常量html=`
`.split(“\n”)
.forEach(行=>{
str+=line.replace(空格、)+'\n';
});
console.log(str);
}

您可以
匹配新行后面的空格并获得第一行的长度。因此,您可以构建一个正则表达式来替换它们:

let MyFunction=function(str){
让matches=str.match(+/+/g)
让initial=匹配[0]。长度//缩进有多大
让re=RegExp(`\n{${initial}}},'g')//删除行开头的这些
返回str.replace(re,“\n”)
}
设str=`
一些文本
`  

log(MyFunction(str))
您可以
匹配新行后面的空格并获取第一行的长度。因此,您可以构建一个正则表达式来替换它们:

let MyFunction=function(str){
让matches=str.match(+/+/g)
让initial=匹配[0]。长度//缩进有多大
让re=RegExp(`\n{${initial}}},'g')//删除行开头的这些
返回str.replace(re,“\n”)
}
设str=`
一些文本
`  

console.log(MyFunction(str))
因为您希望
html
标记与左侧齐平,但仍然希望
相对缩进,所以您需要的是将每行开头的4个空格替换为空字符串

string.replace(/^ {4}/gm, '')
这是现场代码

MyFunction=function(){
console.log(`
`。取代(/^{4}/gm,”);
}

MyFunction()
由于您希望
html
标记与左侧齐平,但仍然希望
相对缩进,因此您需要的是将每行开头的4个空格替换为空字符串

string.replace(/^ {4}/gm, '')
这是现场代码

MyFunction=function(){
console.log(`
`。取代(/^{4}/gm,”);
}

MyFunction()使用简单的正则表达式查找字符串的初始缩进可以通过
/^[\r\n]?(\s+/
)完成-结果可以用于再次进行初始缩进的逐行替换。这还利用了JS RegExp的性能

如果还希望删除第一个和最后一个换行符,只需在替换字符串后添加
.trim()

函数trimLeadingWS(str){
/*
获取初始缩进
但是忽略新行字符
*/
变量匹配器=/^[\r\n]?(\s+/;
if(匹配测试(str)){
/*
替换初始空格
全局和多行
*/
返回str.replace(newregexp(“^”+str.match(matcher)[1],“gm”),”);
}否则{
//正则表达式不匹配,因此返回原始字符串
返回str;
}
};
MyFunction=函数(){
log(trimLeadingWS(“无需修剪”);
控制台日志(trimLeadingWS)(`
真正地
相当地
一些
of(应忽略换行后的空格数)
缩进
到
去除
`));
控制台日志(trimLeadi)