Javascript 如何从给定的 html 字符串中删除前导和尾随空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10032024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:34:22  来源:igfitidea点击:

How to remove leading and trailing white spaces from a given html string?

javascriptremoving-whitespace

提问by Sunil

I've the following HTML string. What would be sample code in JavaScript to remove leading and trailing white spaces from this string?

我有以下 HTML 字符串。JavaScript 中用于从该字符串中删除前导和尾随空格的示例代码是什么?

<p>&nbsp;&nbsp;</p>
<div>&nbsp;</div>
Trimming using JavaScript<br />
<br />
<br />
<br />
all leading and trailing white spaces
<p>&nbsp;&nbsp;</p>
<div>&nbsp;</div>

回答by Chris Baker

See the String method trim()- https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/Trim

查看字符串方法trim()- https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/Trim

var myString = '  bunch    of <br> string data with<p>trailing</p> and leading space   ';
myString = myString.trim();
// or myString = String.trim(myString);

Edit

编辑

As noted in other comments, it is possible to use the regex approach. The trimmethod is effectively just an alias for a regex:

如其他评论中所述,可以使用正则表达式方法。该trim方法实际上只是正则表达式的别名:

if(!String.prototype.trim) {  
  String.prototype.trim = function () {  
    return this.replace(/^\s+|\s+$/g,'');  
  };  
} 

... this will inject the method into the native prototype for those browsers who are still swimming in the shallow end of the pool.

...这会将方法注入到那些仍在池底游泳的浏览器的原生原型中。

回答by Jhankar Mahbub

var str = "  my awesome string   "
str.trim();    

for old browsers, use regex

对于旧浏览器,请使用正则表达式

str = str.replace(/^[ ]+|[ ]+$/g,'')
//str = "my awesome string" 

回答by Sandeep Manne

string.replace(/^\s+|\s+$/g, "");

回答by Jaime Gómez

If you're working with a multiline string, like a code file:

如果您使用的是多行字符串,例如代码文件:

<html>
    <title>test</title>
    <body>
        <h1>test</h1>
    </body>
</html>

And want to replace all leading lines, to get this result:

并希望替换所有前导行,以获得以下结果:

<html>
<title>test</title>
<body>
<h1>test</h1>
</body>
</html>

You must add the multilineflag to your regex, ^and $match line by line:

您必须将multiline标志添加到正则表达式中,^$逐行匹配:

string.replace(/^\s+|\s+$/gm, '');

Relevant quote from docs:

来自文档的相关引用:

The "m" flag indicates that a multiline input string should be treated as multiple lines. For example, if "m" is used, "^" and "$" change from matching at only the start or end of the entire string to the start or end of any line within the string.

“m”标志表示多行输入字符串应被视为多行。例如,如果使用“m”,“^”和“$”将从仅匹配整个字符串的开头或结尾更改为字符串中任何行的开头或结尾。

回答by Anthony

I know this is a very old question but it still doesn't have an accepted answer. I see that you want the following removed: html tags that are "empty" and white spaces based on an html string.

我知道这是一个非常古老的问题,但它仍然没有一个公认的答案。我看到您希望删除以下内容:基于 html 字符串的“空”和空格的 html 标签。

I have come up with a solution based on your comment for the output you are looking for:

我根据您对您正在寻找的输出的评论提出了一个解决方案:

Trimming using JavaScript<br /><br /><br /><br />all leading and trailing white spaces 

var str = "<p>&nbsp;&nbsp;</p><div>&nbsp;</div>Trimming using JavaScript<br /><br /><br /><br />all leading and trailing white spaces<p>&nbsp;&nbsp;</p><div>&nbsp;</div>";
console.log(str.trim().replace(/&nbsp;/g, '').replace(/<[^\/>][^>]*><\/[^>]+>/g, ""));

.trim()removes leading and trailing whitespace

.trim()删除前导和尾随空格

.replace(/&nbsp;/g, '')removes &nbsp;

.replace(/&nbsp;/g, '')移除 &nbsp;

.replace(/<[^\/>][^>]*><\/[^>]+>/g, ""));removes empty tags

.replace(/<[^\/>][^>]*><\/[^>]+>/g, ""));删除空标签

回答by Chamila Maddumage

01). If you need to remove only leading and trailing white space use this:

01)。如果您只需要删除前导和尾随空格,请使用以下命令:

var address = "  No.255 Colombo  "
address.replace(/^[ ]+|[ ]+$/g,'');

this will return string "No.255 Colombo"

这将返回字符串“No.255 Colombo”

02). If you need to remove all the white space use this:

02)。如果您需要删除所有空白,请使用以下命令:

var address = "  No.255 Colombo  "
address.replace(/\s/g,"");

this will return string "No.255Colombo"

这将返回字符串“No.255Colombo”

回答by SenorAmor

var trim = your_string.replace(/^\s+|\s+$/g, '');