I have the following html string. What would be sample code in JavaScript to remove leading and trailing white spaces from this string?
<p> </p>
<div> </div>
Trimming using JavaScript<br />
<br />
<br />
<br />
all leading and trailing white spaces
<p> </p>
<div> </div>
var str = ' \r \t \n Remove \n leading and \t trailing white spaces \r \t \n ';
String.prototype.singleSpace = function( ) {
return this.replace( new RegExp(/^[\s]+$/, 'gm'), '' ).replace( new RegExp('\\s+', 'gm'), ' ' );
}
console.log('Single Space B/W Words :', str.singleSpace().toUpperCase() );
some RegExp samples Left:{/^[\s]+/g}
Right:{/\s+$/g}
Trim
:{/^\s+|\s+$/g} - Yash 2016-04-27 14:10
See the String method 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 trim
method is effectively just an alias for a regex:
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.
br
tags" - Chris Baker 2013-09-03 15:45
's making the elements empty, there are a few approaches one could use, Anthony's is one. I don't support the use of regex on HTML so I wouldn't use Anthony's approach (I favor of DOM manipulation), but he's the only one who approached the OP's problem correctly understood. I left my answer up since it seems to be helpful, but it isn't the answer to the OP. I'm fine with how it works - Chris Baker 2018-10-02 20:38
var str = " my awesome string "
str.trim();
for old browsers, use regex
str = str.replace(/^[ ]+|[ ]+$/g,'')
//str = "my awesome string"
[\t\n\ ]
or just \s
instead of [ ]
which makes no sense - erik 2015-08-11 14:31
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 multiline
flag to your regex, ^
and $
match line by line:
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.
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.
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> </p><div> </div>Trimming using JavaScript<br /><br /><br /><br />all leading and trailing white spaces<p> </p><div> </div>";
console.log(str.trim().replace(/ /g, '').replace(/<[^\/>][^>]*><\/[^>]+>/g, ""));
.trim()
removes leading and trailing whitespace
.replace(/ /g, '')
removes
.replace(/<[^\/>][^>]*><\/[^>]+>/g, ""));
removes empty tags
var trim = your_string.replace(/^\s+|\s+$/g, '');
01). If you need to remove only leading and trailing white space use this:
var address = " No.255 Colombo "
address.replace(/^[ ]+|[ ]+$/g,'');
this will return string "No.255 Colombo"
02). If you need to remove all the white space use this:
var address = " No.255 Colombo "
address.replace(/\s/g,"");
this will return string "No.255Colombo"